drm30963 2016-02-06 16:22
浏览 157
已采纳

我已完成AngularJS代码但数据未显示

Please help me to find my problem on my coding, I have done all the AngularJS code and html and php for the json encoding but I cannot fetch the data in my database... Here is my current code

Index.html

 <body ng-app="myApp" ng-controller="KaryawanCtrl">

<form method="post">
  <input type="text" ng-model="karyawan.nama">
  <input type="text" ng-model="karyawan.alamat">
  <input type="submit" ng-click="tambahData()" value="simpan">
</form>
<table ng-init="dapatkanData()">
    <thead>
    <th>nama</th>
    <th>alamat</th>
    </thead>
    <tr ng-repeat="item in dataKaryawan">
        <td>{{item.nama}}</td>
        <td>{{item.alamat}}</td>
    </tr>
</table>

Here is the angular code

js/app.js

 var app= angular.module("myApp",[]);
  app.controller("KaryawanCtrl",function($scope,$http){
    //variabel awal
    $scope.aksi="tambah";
    $scope.karyawan={};
    //angularjs untuk menyimpan data ke database
    $scope.tambahData = function(){
        $http.post(
          'post.php',
          {
            data: $scope.karyawan
          }
        ).success(function(data){
          alert("data berhasil dimasukkan");
        }).error(function(){
          alert("Gagal menyimpan data");
        });

        //angularJS untuk menampilkan data ke tabel
        $scope.dapatkanData = function(){
          $http.get('karyawan.php').success(function(data){
              $scope.dataKaryawan = data;
          });  
        };

        };
    });

Here is the php

$koneksi = mysqli_connect("localhost","root","","belajar") or die("tidak bisa tersambung ke database");
$perintah_sql = "SELECT * FROM karyawan";

$data = [];
$result = mysqli_query($koneksi,$perintah_sql);

while($row= mysqli_fetch_array($result)){
  $temp_data = [];
  $temp_data['nama']= $row['nama'];
  $temp_data['alamat']=$row['alamat'];
  array_push($data,$temp_data);
}

echo json_encode($data);
  • 写回答

2条回答 默认 最新

  • dshkmamau65777662 2016-02-06 18:04
    关注

    The problem is that your dapatkanData function is defined inside of the tambahData function. Just move it to the outside like this and it should work:

    var app= angular.module("myApp",[]);
    
    app.controller("KaryawanCtrl",function($scope,$http){
       //variabel awal
       $scope.aksi="tambah";
       $scope.karyawan={};
    
       $scope.tambahData = function(){
           $http.post('post.php',{ data: $scope.karyawan }).success(function(data){
             alert("data berhasil dimasukkan");
           }).error(function(){
             alert("Gagal menyimpan data");
           });
        };
    
        $scope.dapatkanData = function(){
          $http.get('karyawan.php').success(function(data){
              $scope.dataKaryawan = data;
          });  
        };
    
        $scope.dapatkanData(); // Use this instead of ng-init. Remove that from the html
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题