Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs using variables in templateUrl

I'm trying to use $scope in my templateUrl like this:

app.config(['$routeProvider',
function ($routeProvider) {
    $routeProvider.
        when('/blog', {
                templateUrl: 'themes/{{ mainCtrl.site_theme }}/view/home.html',
                controller: 'BlogMain',
        }).
        otherwise({
            redirectTo: '/blog'
        })
}]);

When trying to use:

root/themes/<theme_name>/view/home.html as the template file. It currently gives me the error GET http://www.url.com/themes/%7B%7B%20mainCtrl.site_theme%20%7D%7D/view/home.html 404 (Not Found)

NOTE: it works fine if I type the theme name normally

Any help will be appreciated :D thanks in advance

like image 363
Ayce Allchorn Avatar asked Jul 01 '26 18:07

Ayce Allchorn


1 Answers

OPTION ONE

You can set a variable (in your case the theme name) in the url and then access it in your routes:

.when('/blog/:themeName', {
     templateUrl: function(params) {
         return 'themes/'+ params.themeName +'/view/home.html',
     }

This probably less suited to your situation though as it isn't ideal passing your theme name via the URL. I'd recommend option two...

OPTION TWO

You can use a provider to allow the setting of the theme name:

app.provider('themeConfig', function() {
    this.name = 'Default';
    this.$get = function() {
        var name = this.name;
        return name;
    };
    this.setName = function(name) {
        this.name = name;
    };
});

You can now inject the provider into your application config, set the theme and then use it in your routes.

app.config(['$routeProvider', 'themeConfigProvider', 
   function ($routeProvider, themeConfigProvider) {

      themeConfigProvider.setName('mytheme');

      $routeProvider
          .when( '/this', { templateUrl: themeConfigProvider.name + '-this.html' })
          .when( '/that', { templateUrl: themeConfigProvider.name +'-that.html' })
          .when( '/other', { templateUrl: themeConfigProvider.name +'-other.html' })
          .otherwise( { redirectTo: '/this' });
}]);

Working jsfiddle here: http://jsfiddle.net/3B5Sk/

like image 106
rdjs Avatar answered Jul 03 '26 08:07

rdjs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!