将参数传递给解析函数

Passing parameters to a resolve function

我无法弄清楚如何将参数传递给作为 ngRoute 解析一部分的函数。

就我而言,我正在用令牌做事。这些令牌是键入的,因此您不能使用相同的令牌来确认和发送电子邮件以及重置密码。以下是我的路线定义方式:

.when("/confirm/:token", {
    controller: "confirmEmailController",
    templateUrl: "/app/views/confirmEmail.html",
    resolve: {
        tokenStatus: getTokenStatus
    }
})
.when("/reset/:token", {
    controller: "resetPasswordController",
    templateUrl: "/app/views/resetPasswordEmail.html",
    resolve: {
        tokenStatus: getTokenStatus
    }
})

这是为它们调用的 getTokenStatus 函数:

var getTokenStatus = ["$q", "$route", "tokenService", function($q, $route, tokenService)
{    
    var deferred = $q.defer();

    var tokenType = ???? //<-- how do I pass this?

    tokenService
        .getTokenStatus($route.current.params.token, tokenType)
        .success(function(response)
        {                    
            deferred.resolve(true);
        })
        .error(function()
        {
            deferred.resolve(false);
        });

    return deferred.promise;
}];

问题是为了避免代码重复,我需要以某种方式传递令牌类型的值,如代码中标记的那样。我该怎么做?

过去 2 小时我一直在搞这个,但似乎无法弄清楚。

一种方法是在您的 getTokenStatus 服务上放置一个函数。 这是一个简化的示例,但它显示了如何将参数传递给您的解析函数。

app.factory('getTokenStatus',['$q', '$timeout', '$route', function($q, $timeout, $route){
  this.action = function(tokenType) {
    var defer = $q.defer();

    $timeout(function(){
      var res = {
        path: $route.current.params.token,
        tokenType: tokenType
      }
      defer.resolve(res);
    },1000);

    return defer.promise;
  }

  return this;
}]);

并从您的解析对象中调用它:

app.config(function($routeProvider){
  $routeProvider
  .when("/123/:token", {
    template: "<h1>hello</h1>",
    controller: 'testCtrl',
    resolve: {
      tokenStatus: function(getTokenStatus) {
        return getTokenStatus.action('firstToken').then(function(res){
          console.log(res);

        });
      }
    }
})

这里有一个plnkr

1.您可以尝试将令牌类型包含到 route

.when("/:tokenType/:token", {
    controller: "confirmEmailController",
    templateUrl: "/app/views/confirmEmail.html",
    resolve: {
        tokenStatus: getTokenStatus
    }
})
.when("/:tokenType/:token", {
    controller: "resetPasswordController",
    templateUrl: "/app/views/resetPasswordEmail.html",
    resolve: {
        tokenStatus: getTokenStatus
    }
})

然后从 $route.current.params.tokenType 获取它。但这不是干净的解决方案 - 您应该检查 URL 的有效性。

2。您可以使用函数包装

$routeProvider.when("/confirm/:token", {
    controller: "confirmEmailController",
    templateUrl: "/app/views/confirmEmail.html",
    resolve: {
        tokenStatus: getTokenStatus("confirm")
    }
})
.when("/reset/:token", {
    controller: "resetPasswordController",
    templateUrl: "/app/views/resetPasswordEmail.html",
    resolve: {
        tokenStatus: getTokenStatus("reset")
    }
});

var getTokenStatus = function(tokenType) {
    return ["$q", "$route", "tokenService", function($q, $route, tokenService) {
        var deferred = $q.defer();
        tokenService
            .getTokenStatus($route.current.params.token, tokenType)
            .success(function(response)
            {                    
                deferred.resolve(true);
            })
            .error(function()
            {
                deferred.resolve(false);
            });
        return deferred.promise;
    }];
};

3。您可以将获取令牌状态逻辑移至单独的服务

$routeProvider.when("/confirm/:token", {
    controller: "confirmEmailController",
    templateUrl: "/app/views/confirmEmail.html",
    resolve: {
        tokenStatus: ['tokenStatusGetterService', function(tokenStatusGetterService){
            return tokenStatusGetterService("confirm");
        }]
    }
})
.when("/reset/:token", {
    controller: "resetPasswordController",
    templateUrl: "/app/views/resetPasswordEmail.html",
    resolve: {
        tokenStatus: ['tokenStatusGetterService', function(tokenStatusGetterService){
            return tokenStatusGetterService("reset");
        }]
    }
});

//...

.service('tokenStatusGetterService', ["$q", "$route", "tokenService", function($q, $route, tokenService) {
    return function(tokenType) {
        var deferred = $q.defer();
        tokenService
            .getTokenStatus($route.current.params.token, tokenType)
            .success(function(response)
            {                    
                deferred.resolve(true);
            })
            .error(function()
            {
                deferred.resolve(false);
            });
        return deferred.promise;
    };
}]);