pasquers
pasquers

Reputation: 792

Set component TemplateURL to configured path

I am trying to set up a component with a templateURL that is a relative path given a configuration module. However, since it is outside of a controller, I can't figure out how to bring in the dependency to the configuration

app.config.js:

angular.module('app.config', [])
    .constant('config', {
     TEMPLATES_URL: 'js_2/templates/',
     COMPONENTS_URL: 'js_2/components/'

})

home.app.js :

angular.module('home.app', ['rest.service', 'app.config']);

home.component.js:

angular.module('home.app').component('home', {
    /* HOW DO I INJECT THE 'config' DEPENDENCY */
templateUrl: config.TEMPLATES_URL + 'home.template.html',
controller: ....})

I know that when injecting into a controller you would simply inject 'config' and use it, but I don't know how to do this to a component

Thank you

Upvotes: 0

Views: 863

Answers (1)

kevinius
kevinius

Reputation: 4610

You need to set up a function on the templateUrl that injects your service and returns the url, like this:

  angular.module('fsad').component('fsadLeefloon', {
    bindings: {
      onChanges: '&?'
    },
    templateUrl: function(appConstant){return appConstant.paths.modules.fsad + 'leefloon/fsad-leefloon.html'},
    controller: controller
  });

Also, this structure guide may help you set up a good component style.

Upvotes: 0

Related Questions