简体   繁体   English

我可以动态更改 angular 2 中的 templateUrl 吗?

[英]Am I able to change templateUrl in angular 2 dynamically?

I want to change my templateUrl dynamically, I've read Dynamic template URLs in Angular 2 .我想动态更改我的 templateUrl,我已经阅读了 Angular 2 中的动态模板 URL Yes, I can change my template before the component initialize.是的,我可以在组件初始化之前更改我的模板。 But what I want is when I hit a button and the url change, then my html is update, like two way data binding.但是我想要的是当我点击一个按钮并且 url 发生变化时,我的 html 就会更新,就像双向数据绑定一样。

I try make to my component like this:我尝试像这样制作我的组件:

templateUrl: (function() {
    return dynamicURl;
}())

But when change the "dynamicUrl", not thing happened.但是当更改“dynamicUrl”时,没有发生任何事情。 I also use dynamicComponentLoader load my component again and component is not changed.我也使用 dynamicComponentLoader 再次加载我的组件并且组件没有改变。

There is no way you can do it in angular 2 (alpha 48 version) as of now.到目前为止,您无法在 angular 2(alpha 48 版本)中做到这一点。 You can refer to : https://groups.google.com/forum/#!topic/angular/ROkKDHboWoA ,你可以参考: https : //groups.google.com/forum/#!topic/angular/ROkKDHboWoA

https://github.com/angular/angular/issues/2753 https://github.com/angular/angular/issues/2753

There is a similar question : Dynamic template in templatURL in angular2有一个类似的问题: angular2 中 templatURL 中的动态模板

As it seems that they are not planning to include this, you would like to write your own function to populate the div with dynamic content.由于他们似乎不打算包含此内容,因此您想编写自己的函数来用动态内容填充 div。 Or a similar attribute directive.或类似的属性指令。

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

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