我是 AngularJS 的新手,在加载数据时无法获得资源承诺重新渲染视图。这导致 {{}} 被替换为空白(来自承诺),但视图保持不变。
我已经看到了很多使用 $apply、$digest、$watch 等解决 promise 的问题的例子,并且对这些建议持开放态度,我很想知道我的方法的根本问题。这是我的控制器代码
<div ng-controller='pook'>
<p>There are {{posts.length}} posts</p>
<div ng-repeat='post in posts'>
<h3>{{post.title}}</h3>
<div>{{post.text}}</div>
<a href='/readPost/{{post.id}}'>More</a>
| -
<a href='/editPost/{{post.id}}'>Edit</a>
| -
<a href='/deletePost/{{post.id}}'>Delete</a>
</div>
</div>
<script type='text/javascript'>function pook($scope, $http, $route, $routeParams, $resource)
{
/*Approach #1 does work
$http.post('/').success(function(data, status, headers, config)
{
$scope.posts = data.posts;
});
*/
var User = $resource('/');
/*Approach #2 does work
User.save('', function(data)
{
$scope.posts = data.posts;
})
*/
//Approach #3 does NOT work -> renders blanks and never re-renders on loaded data
$scope.posts = User.save().posts
}
</script>
我加载的脚本是
//ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js' //ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular-resource.min.js'
我想要的承诺行为的描述在这里 http://docs.angularjs.org/api/ngResource .$resource