Angularjs-添加新的创建数据后,结果不会更新
我知道这里有人问过,但我仍然被这个问题所困扰,在我向数据库添加新数据后,结果没有更新 我有一个用于添加和检索数据的控制器Angularjs-添加新的创建数据后,结果不会更新,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我知道这里有人问过,但我仍然被这个问题所困扰,在我向数据库添加新数据后,结果没有更新 我有一个用于添加和检索数据的控制器 .controller('GameCtrl', ['$scope', function(scope) { scope.createGame = function() { Api.createGame(postData).then(function(result) { console.log(result); scope.$broadca
.controller('GameCtrl', ['$scope', function(scope) {
scope.createGame = function() {
Api.createGame(postData).then(function(result) {
console.log(result);
scope.$broadcast("event: list updated", true);
}, function(result) {
console.log(result.data.message);
scope.showError = true;
scope.data = 'Warning! '+result.data.message;
});
};
var quizmaster = {};
quizmaster.gameList = function() {
Api.getGameList().then(function(result) {
console.log(result.data);
scope.games = [];
scope.games = result.data;
}, function(result) {
console.log(result);
});
};
scope.$on("event: list updated", function(value) {
console.log('event update');
quizmaster.gameList();
});
}]);
广播事件工作正常,并在添加后调用quizmaster.gameList()函数加载游戏列表。控制台中显示的数据没有更新,新创建的数据也没有更新。但是当我查看我的数据库时,新添加的数据已经存储。只需刷新页面即可更新列表。如何在不刷新页面的情况下在视图中反映添加的数据
谢谢你的帮助
更新:Api.getGameList()是一项服务
.factory('Api', function($http) {
return {
getGameList: function(params) {
return $http({
method : 'GET',
url : api + 'games/list',
params : params,
headers : {'X-GameApp-Token' : credential.token}
});
}
}
});
什么是Api.getGameList()?如果是在angular之外完成的(即then()不是来自$http承诺),则必须将更改通知angular。在$scope.apply()中包装设置数据:
我更新了上面的代码,其中Api.getGameList()是一个服务,请检查。谢谢如果console.log(result.data)
未显示新数据,则您的web服务不会返回新数据。我认为您应该发布一个问题,显示可能出现故障的服务器端代码。在Chrome中,您可以检查dev tools中的network选项卡,以验证您没有得到所需的响应。
$scope.$apply(function() {
scope.games = result.data;
});