我遵循思想家平均堆栈教程,并且在角工厂服务方面有问题。
angular.js:11598错误:达到$rootScope:infdig 10 $digest()迭代。流产!在最近5次迭代中触发的观察者:[]
app.js
app.factory('posts', ['$http', function($http){
var o = {
posts: []
};
o.getAll = function() {
return $http.get('/posts').success(function(data){
console.log(data)
angular.copy(data, o.posts);
});
};
return o;
}]);
我的配置文件有路由提供程序
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainCtrl',
resolve: {
post: ['$stateParams', 'posts', function($stateParams, posts) {
return posts.get($stateParams.id);
}]
}
})
我不知道出了什么问题。
任何帮助都是非常感谢的。提前谢谢..。
发布于 2016-04-22 09:13:37
不推荐
.success
,所以我将使用then
我想这就是你想写的。
app.factory('posts', ['$http', function($http){
var o = {};
o.get = function(id){
return $http.get('/posts/'+id).then(function(response){
return response.data;
});
}
o.getAll = function() {
return $http.get('/posts').then(function(response){
return response.data;
});
};
return o;
}]);
resolve: {
post: ['$stateParams', 'posts', function($stateParams, posts) {
return posts.get($stateParams.id);
}]
}
// usage of the factory in controller :
posts.getAll().then(function(posts){
$scope.allPosts = posts;
})
posts.get(id).then(function(post){
$scope.post = post;
})
以下几点:
then
/ success
是可链式的;但是,您必须使用return语句,这样下一个链才会有数据。我要你归还的东西。return posts.get($stateParams.id);
,所以我添加了一些相关的东西。https://stackoverflow.com/questions/36789441
复制相似问题