将参数传递给解析函数
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;
}];
问题是,为了避免代码重复,我需要以某种方式传递代码中标记的令牌类型的值。我怎么能那样做?
在过去的两个小时里,我一直在处理这个问题,但似乎无法解决。
1。您可以尝试将令牌类型包括在路由中
.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;
};
}]);
一种方法是在getTokenStatus服务上放置一个函数。这是一个简化的示例,但它展示了如何将参数传递给resolve函数。
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
相关文章:
- 在构造控制器、服务等时作为函数参数传递之前列出的角度 JS 变量
- 作为函数参数传递的匿名函数的范围
- 通过函数参数传递id
- 将数组值作为函数参数传递
- 是否需要初始化在 Javascript 中作为“类”构造函数参数传递的变量
- JavaScript 函数参数传递数组
- Angular2 将函数参数传递给构造函数
- 为什么我不能将角度变量作为函数参数传递
- jQuery - 基于作为函数参数传递的对象构建动态 if 语句
- 停止对作为函数参数传递到新列表的对象引用
- 在 JavaScript 中将数组作为内置函数参数传递
- AngularJS将范围变量名称作为函数参数传递
- Javascript 将对象作为函数参数传递
- 在对象上调用方法或将对象作为函数参数传递的差异
- 正确的方法:始终将所有变量作为函数参数传递
- 如何将动态创建的复选框值作为函数参数传递
- 节点简斯 |将对象作为函数参数传递
- 什么是将对象属性作为函数参数传递的良好策略/模式
- 将对象作为函数参数传递.不确定如何访问这些值
- 为什么我们要将函数参数传递到$scope$申请