AngularJS $ resource参数不会被其实际值替换?

时间:2013-01-25 13:06:33

标签: javascript angularjs

我最近决定使用$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});
};

我错过了什么吗?

1 个答案:

答案 0 :(得分: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