2016-06-22 12 views
2

ist es möglich, eine AngularJS Komponente mit einem dynamischen templateUrl zu erstellen? Means Ich möchte einen Dienst in der Komponente injizieren, die mir einen Basispfad der Vorlage gibt:angularjs Komponente mit dynamischem templateUrl

das heißt: templateUrl: configuration.baseScriptPath + ‚... html‘

Es ist möglich, mit einer Richtlinie, aber wie geht das mit einer Komponente?

angular.module('runtime') 
    .component('textInput', { 

     templateUrl: /*configuration.baseScriptPath + */'fd/components/text_input_instance.html', 
     controller: [ 
      '$scope', '$element', 'focusInputGroup', 'configuration', 
      function ($scope, $element, focusInputGroup, configuration) { 
+0

Wenn es in der Richtlinie arbeitet, sollte es auch in der Komponente arbeiten, weil ‚templateUrl‘ Eigenschaft nicht geändert .. –

+0

I Konfiguration injizieren wollen, so dass es in templateUrl verwendbar ist. – dec

+0

Können Sie Ihren Komponentencode teilen? –

Antwort

5

Statt templateUrl können Sie Schablone und ng-include wie folgt verwenden:

angular.module('runtime') 
    .component('textInput', { 

     template: '<div ng-include="getTemplate()">', 
     controller: [ 
      '$scope', '$element', 'focusInputGroup', 'configuration', 
      function ($scope, $element, focusInputGroup, configuration) { 

      $scope.getTemplate = function() { 
       return configuration.baseScriptPath + 'fd/components/text_input_instance.html'; 
      }; 
+0

Es ist keine saubere Lösung, aber funktioniert einfach, danke – PSA

2

templateUrl Eigenschaft der Komponente nimmt auch eine Funktion. so können Sie dies versuchen,

angular.module('runtime') 
    .component('textInput', { 

     templateUrl: ['$configuration', function($configuration) { 
     return $configuration.basepath + '.html' 
     }], 
     controller: [ 
      '$scope', '$element', 'focusInputGroup', 'configuration', 
      function($scope, $element, focusInputGroup, configuration) { 
+1

Sollte 'templateUrl: [...]' sein, sonst wird nur der HTML-Template-Dateiname gedruckt, da 'template' eine Zeichenkette ist. –

+0

korrekt. Ich verwende . –

Verwandte Themen