javascript - Angular 资源更新不起作用

标签 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'}}); 
});

还有一些 Controller 来处理所有更新操作。

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 抛出前面提到的错误消息。

最佳答案

想通了。 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'}
  })  
});

关于javascript - Angular 资源更新不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17734749/

相关文章:

javascript - 选择菜单更改不起作用

Javascript 原型(prototype)与通用函数 - 性能/可读性

javascript - 如何使用 ng-options 遍历对象中的数组?

javascript - Angular Resource - 如何检查资源实例是否有任何未保存的更改?

angularjs - 发布请求后使 $resource 缓存无效

javascript - 如何将react-google-maps中的lat/lng附加到this.state.markers

javascript - 数字和加号的正则表达式

angularjs - 如何使用包裹在 CKEditor 小部件中的 Angular 指令

javascript - 让一个 Controller 中的 radio 隐藏/显示另一个 Controller 中的元素

javascript - Angularjs 资源多参数