1

I have the following route definition:

.......... 
when('/:templateFile', 
{
  templateUrl: function (param) { return 'views/' + param.templateFile + '.html' }
})
..........

The next piece of code listens to the route is changing. If the user is not authenticated and the next page/template to which he wants to navigate is not the login page, then the user is redirected to login page.

Everything works fine except that the next.templateUrl value is actually function (param) { return 'views/' + param.templateFile + '.html' instead of views/login.html, for example.

alert(next.templateUrl) will display function (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");
            }
        }
    });
});

Any ideas how I can get the next templateurl when using dynamic templates?

1 Answer 1

1

Well since templateUrl is indeed a function you can try to use it as function. If you invoke it with route parameters it should return a template 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");
            }
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.