修改 restangular 服务路径

Modify restangular service path

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

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 下。但我不知道如何将 /change_password 添加到现有的 restangular 服务中?

您可以将工厂修改为如下所示

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");
    });