剛剛開始角度。我成功地使用php保存和檢索數據庫中的數據。我也能循環的數據列表項目中,但是當我添加新用戶,當我刷新瀏覽器,它顯示新添加的用戶角度循環不更新
這是我的HTML代碼列表不會只更新:
<div>
<ul ng-init="get_user()">
<li ng-repeat="user in userInfo ">{{user.user_name}}<a href="" ng-click="prod_delete(product.id)"> --> Delete</a></li>
</ul>
</div>
這是我的app.js代碼:
var app = angular.module('AddUser', ['ui.bootstrap']);
app.controller('AppCtrl', function($scope, $http){
$scope.userInfo = [];
/** function to add details for a user in mysql referecing php **/
$scope.save_user = function() {
$http.post('db.php?action=add_user',
{
'user_name' : $scope.user_name,
'user_email' : $scope.user_email
}
)
.success(function (data, status, headers, config) {
$scope.userInfo.push(data);
console.log("The user has been added successfully to the DB");
console.log(data);
})
.error(function(data, status, headers, config) {
console.log("Failed to add the user to DB");
});
}
/** function to get info of user added in mysql referencing php **/
$scope.get_user = function() {
$http.get('db.php?action=get_user').success(function(data)
{
$scope.userInfo = data;
console.log("You were succesfull in show user info");
//console.log(data);
})
}
});
應該叫'$ scope.get_user()'方法來再次從數據庫提取數據 –
OP作爲parkar說,這應作爲NG-INIT只調用一次。 – squiroid
但GY22正在將'data'推入'userInfo'成功$ http.post –