最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS push to array retrieved by $resource query and save - Stack Overflow

programmeradmin1浏览0评论

Checkout the code below. The question is in the ments.

angular.module('MainStreetMower.services', ['ngResource'])
.factory('Videos', function($resource) {
    return $resource('/api/jobs/1/');
});
function VideoListCtrl($scope, Videos) {
    $scope.videos = Videos.query();
    $scope.what = function() {
        // proper way to push to the videos array and $save() the new array.
    }
}

Checkout the code below. The question is in the ments.

angular.module('MainStreetMower.services', ['ngResource'])
.factory('Videos', function($resource) {
    return $resource('/api/jobs/1/');
});
function VideoListCtrl($scope, Videos) {
    $scope.videos = Videos.query();
    $scope.what = function() {
        // proper way to push to the videos array and $save() the new array.
    }
}
Share Improve this question asked Jan 3, 2013 at 22:16 drew schmaltzdrew schmaltz 1,5844 gold badges19 silver badges29 bronze badges 1
  • 2 I think you need to describe what you want more clearly – Neil Commented Jan 4, 2013 at 9:48
Add a ment  | 

1 Answer 1

Reset to default 8

I would say the following:

function VideoListCtrl($scope, Videos) {
    $scope.videos = Videos.query();

    $scope.what = function() {

        var newVideoData = {}; // prepare new video data here from the model
        new Videos(newVideoData).$save(function(video){
          $scope.videos.push(video);
        }); 

    }
}

if you don't want to refresh the whole list. Alternatively you could re-query the collection in the save callback is you expect changes from other sources:

new Videos(newVideoData).$save(function(video){
   $scope.videos = Videos.query();
});

Please note that you could use the save method on the class level. For example, the code above could re-written as:

Videos.save(newVideoData, function(video){
   $scope.videos = Videos.query();
});
发布评论

评论列表(0)

  1. 暂无评论