1

我有一个角度组件,我正在 angular 1.5 中构建。它看起来像这样:

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 的提供程序,但我无法在组件定义时注入它。有没有办法通过绑定获取 URL 或稍后在控制器中设置它?

4

1 回答 1

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...
    }
}
于 2016-06-24T19:59:08.687 回答