Javascript 我已经完成了AngularJS代码,但是数据没有显示出来
请帮我找到我的编码问题,我已经完成了所有的AngularJS代码以及json编码的html和php,但我无法获取数据库中的数据。。。这是我目前的代码 Index.htmlJavascript 我已经完成了AngularJS代码,但是数据没有显示出来,javascript,php,angularjs,json,Javascript,Php,Angularjs,Json,请帮我找到我的编码问题,我已经完成了所有的AngularJS代码以及json编码的html和php,但我无法获取数据库中的数据。。。这是我目前的代码 Index.html <body ng-app="myApp" ng-controller="KaryawanCtrl"> <form method="post"> <input type="text" ng-model="karyawan.nama"> <input type="text" n
<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>
这里是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);
尝试像这样绑定数据
$scope.dataKaryawan = data.data;
或者试着
$scope.$apply()
检索数据后尝试绑定数据,如
$scope.dataKaryawan = data.data;
或者试着
$scope.$apply()
检索数据后,问题在于
dapatkanData
函数是在tambahData
函数中定义的。只要像这样把它移到外面,它就会工作:
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
});
问题在于
dapatkanData
函数是在tambahData
函数中定义的。只要像这样把它移到外面,它就会工作:
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
});
你能告诉我们你得到了什么错误吗?嗨,就像我在标题中提到的,当我访问索引时我的数据没有显示。html…你能告诉我们你得到了什么错误吗?嗨,就像我在标题中提到的,当我访问索引时我的数据没有显示。html…哇,酷,这是我问题的解决方案,谢谢@DerekMT12wow酷,这是我问题的解决方案,谢谢@DerekMT12