我最近决定将$resource
用于REST服务,但很快就遇到了麻烦。我有一个简单的网址:
/ r /接口/激活/:活动/地址/:地址
但是在单元测试中调用它会使它失败,并且:
// 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});
};
我想念什么吗?
最佳答案
我使用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调用:
开机自检... / r / interface / activate / false / addr / 123foobar