1

我有角狀的部件我建立在角1.5。它看起來像這樣:動態分配templateUrl在角分量

var module = angular.module("ncRelationshipSearch"); 

module.component("relationshipSearch", { 
    templateUrl: <need to dynamically assign>, 
    controllerAs: "vm", 
    controller: ['config', function(config){ 
     ... config provider here knows about templateURL... 
    } 
} 

我需要templateUrl以某種方式分配。我有一個config提供商可以提供的網址,但我不能在組件定義的點注入它。有沒有辦法通過綁定來獲取URL,或者以後在控制器設置呢?

回答

3

你可以有templateUrl功能,你可以注入service/factory。實際上,你可以注入config提供商在templateUrl功能。

module.component("relationshipSearch", { 
    templateUrl: function(config){ 
     //play here with service variables generate templateUrl 
     //other code can also lie here. :) 
     return config.myTemplatUrl; 
    }, 
    controllerAs: "vm", 
    controller: ['config', function(config){ 
     ... config provider here knows about templateURL... 
    } 
}