1

我最近决定使用$resourceREST 服务,但很快就遇到了麻烦。我有一个简单的网址:

/r/interface/activate/:active/addr/:address

但是在单元测试中调用它会使其失败:

// expected: POST /r/interface/activate/false/addr/123
// got     : POST /r/interface/activate/false/addr/@address

我定义的资源是:

module.factory('NetworkInterfaceActivationService', function ($resource) {
    return $resource("/r/interface/activate/:active/addr/:address", {}, {
        activate:{method:'POST', params:{active:"true", address:'@address'}, isArray:false},
        deactivate:{method:'POST', params:{active:"false", address:'@address'}, isArray:false}
    });
});

这就是我如何称呼我的服务!

$scope.deactivateNetworkAddress = function (address) {
   NetworkInterfaceActivationService.deactivate({address:address});
};

我错过了什么吗?

4

1 回答 1

0

我用 Angular 1.3 尝试了你的代码,但我无法重现这个问题。

jsFiddle

<button ng-click="parentCtrl.doIt('123foobar')">execute</button>
app.controller('ParentCtrl', function (NetworkInterfaceActivationService) {
    this.doIt = function (address) {
        NetworkInterfaceActivationService.deactivate({address:address});
    };
})

app.factory('NetworkInterfaceActivationService', function ($resource) {
    return $resource("/r/interface/activate/:active/addr/:address", {}, {
        activate:{method:'POST', params:{active:"true", address:'@address'}, isArray:false}
    });
});

控制台中的 HTTP 调用:

POST .../r/interface/activate/false/addr/123foobar

于 2014-09-20T09:16:14.150 回答