Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJs从动态模板URL获取模板名称_Angularjs_Angular Ui Router - Fatal编程技术网

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

非常好!非常感谢!