0
我有一個控制器,它看起來是這樣的:
(函數(){ 「使用嚴格的」;
angular.module("app")
.controller("postsController", postsController);
function postsController($scope, $http, $uibModal, PostService) {
var vm = this;
$scope.posts = [];
PostService.getPosts()
.then(function (p) {
$scope.posts = p.data;
}, function (error) {
vm.errorMessage = "Failed to load data: " + error;
});
$scope.$watchCollection('posts', function(newPosts, oldPosts) {
PostService.getPosts();
});
}
})();
的原始數據從服務中拉出並且posts數組被填充正確但是,如果數組中的數據已經改變,我有刷新視圖的麻煩,或者視圖中沒有任何事情發生,或者我看到$的無限循環看。
任何幫助是偉大的y讚賞。
這裏有幾個問題。首先是你的'$ watchCollection'調用'getPosts'異步方法,但不會對數據做任何事情。另外,您正在將'$ scope'和ControllerAs混合在同一個控制器中,這可能會導致混淆。 – Claies