修改restangular服务路径

时间:2015-03-05 10:29:54

标签: angularjs restangular

现在我正在创建一个这样的服务:

angular.module('test')
.factory('AccountResource', function Account(Restangular) {
    return Restangular.service('accounts');
});

现在路径是/ accounts所以我只是将该服务注入我的一个控制器

.controller('ProfileChangePasswordFormController', function ($scope, AccountResource) {
$scope.changePassword = function(){

        console.log("change passord send request " );
        console.log($scope.password);

        AccountResource.post("chage_password",$scope.password).then(function() {
            console.log("Object saved OK");
        }, function() {
            console.log("There was an error saving");
        });
    };

}

您可以看到我更改密码的其他资源位于/ accounts / change_password下。但我不知道如何将add / change_password添加到现有的restangular服务中?

1 个答案:

答案 0 :(得分:0)

您可以将工厂修改为以下内容

angular.module('test')
 .factory('AccountResource', function Account(Restangular) {
      function accounts(){
        return Restangular.service('accounts');
       }
      function changePassword(){
        return Restangular.service('change_password');
       }
 return {
     accounts:accounts,
     changePassword:changePassword
     }
});     

并在你的控制器中你可以使用它

AccountResource.changePassword.post("chage_password",$scope.password).then(function()      {
        console.log("Object saved OK");
    }, function() {
        console.log("There was an error saving");
    });