Compadre Compadre - 2 months ago 25
AngularJS Question

Angularjs add constant value to directive's templateUrl

I got constant in module :

angular.module('identicaApp.common', ['ngResource', 'ngCookies'])
.constant('ROOT_PATH', '/static/angular/identica/')


so I want to use it in another directive, in templateUrl attribute, something like this:

angular.module('identicaApp.registration.player', ['identicaApp.common'])
.component('registerPlayer', {
templateUrl: ROOT_PATH + 'registration/register_player/register_player.html',


so how can i do this?

Answer

In component templateUrl function you can have dependencies injected and use it to form templateUrl.

Code

angular.module('identicaApp.registration.player', ['identicaApp.common'])
  .component('registerPlayer', { 'registration/register_player/register_player.html',
    templateUrl: function(ROOT_PATH){ //<-- add dependency in function
       return ROOT_PATH + 'registration/register_player/register_player.html',
    },
    ...
  })

Check here in doc where clearly mentioned that templateUrl is injectable.

Comments