例如,我在 AngularJS 中有一个资源
var Posts = $resource('posts/:postId', { postId: '@_id' })
我可以查询所有帖子的列表(数组)
$scope.posts = Posts.query();
我可以在构造函数中创建一个帖子
$scope.post = new Posts({
title: ...,
content: ...
});
但我喜欢但不能做的是:使用该构造函数创建一个帖子列表,例如:
$scope.posts = new Posts([
{
title: ...,
content: ...
},
{
title: ...,
content: ...
},
{
title: ...,
content: ...
},
]);
有了这个,我只能执行 $scope.posts.save()
尝试将整个列表推送到单个帖子并导致错误。是不是可以$scope.posts[...].save()
/$scope.posts[...].remove()
它们单独?
最佳答案
我做了显而易见的事情:
$scope.posts = [];
[
{
title: ...,
content: ...
},
{
title: ...,
content: ...
},
{
title: ...,
content: ...
},
].foreEach(function(post) {
$scope.posts.push(new Posts(post));
});
它完全符合我的要求。
关于javascript - 带数组的 AngularJS 资源构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31413357/