角度资源更新无效

时间:2013-07-18 22:03:58

标签: javascript angularjs angularjs-resource

我为我的2种数据类型Act和Scene构建了2个工厂。除了我的场景更新功能(创建,阅读,删除工作正常)之外,一切都很完美。对于我的生活,我无法弄清楚Scenes-update功能不起作用,我只是得到这样的信息:

TypeError: Object function Resource(value){
        copy(value || {}, this);
      } has no method 'update'
    at Object.ScenesUpdateCtrl.$scope.save

我有2家工厂:

app.factory('ActsService', function($resource) {
  return $resource('/api/acts/:id', {id: '@id'}, {update: {method: 'PUT'}});
});

app.factory('ScenesService', function($resource) {
  $resource('/api/scenes/:id', {id: '@id'}, {update: {method: 'PUT'}});
  $resource('/api/scenes/:actId', {actId: '@id'}, {query: {method: 'GET', isArray: true}});
  $resource('/api/scenes/:actId/:id', {actId: '@actId', id: '@id'}, {get: {method: 'GET'}});
  return $resource('/api/scenes/:actId/:id', {actId: '@actId', id: '@id'}, {delete: {method: 'DELETE'}}); 
});

还有一些控制器可以处理所有的Update操作。

function ActsUpdateCtrl ($scope, $location, $routeParams, ActsService) {
  var id = $routeParams.id
  $scope.act = ActsService.get({id: id})
  $scope.action = "Update"

  $scope.save = function() {
    ActsService.update({id: id}, $scope.act, function() {
      $location.path('/admin/acts')
    })
  }
}

function ScenesUpdateCtrl ($scope, $location, $routeParams, ScenesService) {
  var id = $routeParams.id
  var actId = $routeParams.actId
  ScenesService.get({actId: actId, id: id}, function(resp){
    $scope.scene = resp
    $scope.actId = resp.PartitionKey
        $scope.prev = resp.prev.split(",")
  })
  $scope.scenes = ScenesService.query({actId: actId})

  $scope.action = "Update"

  $scope.save = function() {
    $scope.scene.prev = $scope.prev
    ScenesService.update({id: id}, $scope.scene, function() {
      $location.path('/admin/acts/' + $scope.actId)
    })
  } 
}

Act有效,Scene抛出前面提到的错误信息。

1 个答案:

答案 0 :(得分:2)

想出来。 AngularJS - creating a service object

找到一种方法来编写它,以便它可以工作,不确定它为什么会起作用,但随着它的工作,我很高兴。

app.factory('ScenesService', function ($resource) {
  return $resource('/api/scenes/:actId/:id', { actId : '@actId', id:'@id' }, {
    update: {method: 'PUT'},
    query: {method: 'GET', isArray: true},
    get: {method: 'GET'},
    delete: {method: 'DELETE'}
  })  
});