【问题标题】:Loading Dynamic Url in Component's templateURL Angular 2.0.0-rc.1在组件的 templateURL Angular 2.0.0-rc.1 中加载动态 URL
【发布时间】:2016-10-11 05:07:09
【问题描述】:

有没有办法在templateUrl 中加载动态网址。类似于下面的代码:-

@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: DynamicUrl, //Load DynamicUrl here
    styleUrls: ['app.component.css']
})


export class AppComponent{

    var DynamicUrl= 'app.component.html';

     // ideally this template name will be pulled from the server.

}

我对生命周期感到困惑。在哪里初始化和定义 DynamicUrl 并应用逻辑从服务器获取。

感谢任何形式的帮助。

【问题讨论】:

  • 是的,我以前遇到过。但它涉及在测试版中删除的@View 装饰器。因此我提到了 rc 版本。它需要创建假组件。必须有一些更干净的解决方法。

标签: typescript angular angular2-template angular2-components


【解决方案1】:

也许构建一个与模板字符串一起使用的父上下文对象是可行的。 templateUrl:`${foo}`,

const dynamicUrl = ():string => {
    //...do stuff

    return 'url-string'
}

@Component({
    //...
    templateUrl: `${dynamicURL()}`
})

//...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2016-09-15
    • 2016-09-02
    • 2019-10-19
    • 1970-01-01
    • 2020-04-17
    • 2019-02-06
    相关资源
    最近更新 更多