简体   繁体   English

在角度组件中动态分配templateUrl

[英]Dynamically assign templateUrl in angular component

I have an angular component I am building in angular 1.5. 我有一个角度组件我正在建立1.5角。 It looks like this: 它看起来像这样:

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...
    }
}

I need the templateUrl to be assignable somehow. 我需要以某种方式分配templateUrl。 I have a config provider that could supply the URL but I can't inject it at the point of component definition. 我有一个可以提供URL的config提供程序,但我不能在组件定义时注入它。 Is there a way to get the URL in through the bindings or set it later in the controller? 有没有办法通过绑定获取URL或稍后在控制器中设置它?

You can have function for templateUrl where you can inject service/factory . 您可以使用templateUrl功能,您可以在其中注入service/factory You can actually inject config provider over the templateUrl function. 您实际上可以通过templateUrl函数注入config提供程序。

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...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM