Dynamic templateUrl for $routeProvider

1,737 views
Skip to first unread message

Arnaud Vandecasteele

unread,
Apr 26, 2013, 7:30:19 AM4/26/13
to ang...@googlegroups.com
Hello,

I'm playing with Angular and I'm quite impress by the potentiality of this framework.

But there is still something I don't understand. I want to be able to change the view text depending on the languages used by the person.
When they click on a language (e.g., french, english), the URL change as mywebsite/#language/fr or mywebsite/#/language/en
As the name of my template are al the same (e.g. fr-description.html, en-description.html), is there a way to set a dynamic templateURL instead of defining a new routeProvider for each language ?
For example :

angular.module('OSMQmodule', []).
    config(['$routeProvider', '$locationProvider', function($routeProvider, $routeParams) {   
        $routeProvider.   
            when('/language/:language', {
                templateUrl: "./res/tpl/" + $locationProvider.language + "-description.html",
                controller: OSMQ.CONTROLLERS.OSMQSwitchLanguageCtrl
            }).
             otherwise({redirectTo: '/'});
        }
]);

Thanks for your help

Arnaud

R. X. Rolaveric

unread,
Apr 27, 2013, 6:36:31 AM4/27/13
to ang...@googlegroups.com
Hi Arnaud,

If I'm reading the source correctly, you can set "templateUrl" to a function which accepts the $routeParams as a parameter:
https://github.com/angular/angular.js/blob/master/src/ng/route.js

So in your example, perhaps you could try the following:
templateUrl: function ($routeParams) {
  return "./res/tpl/" + $routeParams.language + "-description.html";
}

Cheers,
RX

Arnaud Vandecasteele

unread,
Apr 29, 2013, 8:42:38 AM4/29/13
to ang...@googlegroups.com
Hi RX,

Thanks for your answer.
I've already tried your solution but I was using the stable AngularJS.
With this one, I got an error but with the dev (unstable) version it works fine !

Thank you again for your help.

Arnaud
Reply all
Reply to author
Forward
0 new messages