5

检查下面的代码。问题在评论中。

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.
    }
}
4

1 回答 1

8

我会说以下内容:

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);
        }); 

    }
}

如果您不想刷新整个列表。或者,您可以在保存回调中重新查询集合,如果您期望来自其他来源的更改:

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

请注意,您可以save在类级别上使用该方法。例如,上面的代码可以重写为:

Videos.save(newVideoData, function(video){
   $scope.videos = Videos.query();
});
于 2013-01-03T22:40:57.393 回答