2

I have a resource in AngularJS, for example

var Posts = $resource('posts/:postId', { postId: '@_id' })

I can query a list (array) of all posts with

$scope.posts = Posts.query();

I can create a single post in the constructor

$scope.post = new Posts({
    title: ...,
    content: ...
});

But what I like to but cant do is: create a list of posts with that constructor, like:

$scope.posts = new Posts([
    {
        title: ...,
        content: ...
    },
    {
        title: ...,
        content: ...
    },
    {
        title: ...,
        content: ...
    },
]);

With this I can only do $scope.posts.save() which tries to push the whole list to a single post and leads to an error. Isn't it possible to $scope.posts[...].save()/$scope.posts[...].remove() them individually?

1 Answer 1

1

I did the obvious:

$scope.posts = [];
[
    {
        title: ...,
        content: ...
    },
    {
        title: ...,
        content: ...
    },
    {
        title: ...,
        content: ...
    },
].foreEach(function(post) {
    $scope.posts.push(new Posts(post));
});

It does exactly what I want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.