2016-11-10 59 views
0

爲同一部件不同的模板網址,我在下面的方式我如何使用角1.5

angular.module('moduleName') 
     .component('myComponent', { 
      templateUrl: 'templatePath1.tpl.html', 
      controller: myController, 
      controllerAs: 'vm', 
      bindings: { 
       b1: '&', 
       b2: '&' 
      } 
     }); 

我使用的是<my-component b1="someThing1" b2="someThing2"></my-component>

現在,我想用實施組件相同的myController與另一個模板位於templatePath2.tpl.html

一種方法是創建另一個組件myComponent2

angular.module('moduleName') 
     .component('myComponent2', { 
      templateUrl: 'templatePath2.tpl.html', 
      controller: myController, 
      controllerAs: 'vm', 
      bindings: { 
       b1: '&', 
       b2: '&' 
      } 
     }); 

有什麼辦法,我可以使用以前的成分,並根據生成的attr選擇templateUrl?如果是,那麼應該怎麼做?

+0

你在創建具有相同的模板URL和控制器mycomponent2得到一個錯誤? – RL89

回答

0

templateUrl可以是一個功能,即獲得了所述元素和屬性作爲參數:

angular.module('moduleName') 
    .component('myComponent', { 
     templateUrl: function(element, attrs) { 
         return 'templatePath' + attrs.x + '.tpl.html'; 
        }, 

<my-component x="1"></my-component>