AngularJs从动态模板URL获取模板名称
我有以下路线定义:AngularJs从动态模板URL获取模板名称,angularjs,angular-ui-router,Angularjs,Angular Ui Router,我有以下路线定义: .......... when('/:templateFile', { templateUrl: function (param) { return 'views/' + param.templateFile + '.html' } }) .......... 下一段代码侦听路由正在更改。如果用户未通过身份验证,并且他想要导航到的下一个页面/模板不是登录页面,则用户将重定向到登录页面 除了next.templateUrl值实际上是function(param){re
..........
when('/:templateFile',
{
templateUrl: function (param) { return 'views/' + param.templateFile + '.html' }
})
..........
下一段代码侦听路由正在更改。如果用户未通过身份验证,并且他想要导航到的下一个页面/模板不是登录页面,则用户将重定向到登录页面
除了next.templateUrl
值实际上是function(param){return'views/'+param.templateFile+'.html'
而不是views/login.html
,其他一切正常
警报(next.templateUrl)将显示函数(param){return'views/'+param.templateFile+'.html'
app.run(function ($rootScope, $location) {
$rootScope.$on("$routeChangeStart", function (event, next, current) {
if (!$rootScope.IsAuth) {
alert(next.templateUrl); // problem
if (next.templateUrl === "views/login.html") {
} else {
$location.path("/login");
}
}
});
});
使用动态模板时,您知道如何获取下一个templateurl吗?既然
templateurl
确实是一个函数,您可以尝试将其用作函数。如果使用路由参数调用它,它应该返回一个模板URL:
app.run(function ($rootScope, $location) {
$rootScope.$on("$routeChangeStart", function (event, next, current) {
if (!$rootScope.IsAuth) {
var templateUrl = next.templateUrl(next.params);
if (templateUrl === "views/login.html") {
// ...
}
else {
$location.path("/login");
}
}
});
});
非常好!非常感谢!