我最近决定使用$resource
进行REST服务,但我很快遇到了麻烦。我有一个简单的网址:
/ 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});
};
我错过了什么吗?
答案 0 :(得分:0)
我使用Angular 1.3尝试了你的代码,但我无法重现这个问题。
<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