2

我有以下代码:

var entityResource = $resource('/api/' + $scope.entityType + '/');

entityResource.delete(entityId,
   function (result) {
      $scope.remove($scope.grid.data, idColumn, entityId);
   }, function (result) {
      alert("Error: " + result);
   })

当代码执行时,我收到以下消息:

DELETE http://127.0.0.1:81/api/Subject 404 (Not Found) 

{"message":"No HTTP resource was found that matches the request URI 'http://127.0.0.1:81/api/Subject'.","messageDetail

我怎样才能做到这一点,以便 $resource 发送带有 /api/Subject/123 的 DELETE 现在它似乎忽略了 entityId 的值

这是我在得到建议后到目前为止所尝试的:

    var entityResource = $resource('/api/:entityType/:entityId',
           { entityType: '@type', entityId: '@id' });
    entityResource.delete({ type: $scope.entityType, id: entityId }, 
        function (result) {
            $scope.remove($scope.grid.data, idColumn, entityId);
            $scope.remove($scope.grid.backup, idColumn, entityId);
            $scope.close();
        }, function (result) {
            alert("Error: " + result);
        })

不幸的是,这给了我:DELETE /api?id=12&type=Subject HTTP/1.1

4

1 回答 1

2

您需要在模板中设置实体 ID:

var entityResource = $resource('/api/:entityType/:entityId', 
                               {type:'@entityType', id: '@entityId'});

然后当你调用它时,传入参数:

entityResource.delete({type: $scope.entityType, id: entityId}, success, failure);

或者

var entity = new entityResource({type: $scope.entityType, id: entityId});
entity.$delete(success, failure);

假设successfailure看起来​​像这样:

var success = function (result) {
    $scope.remove($scope.grid.data, idColumn, entityId);
};

var failure = function (result) {
    alert("Error: " + result);
};
于 2013-09-04T18:31:46.180 回答