简体   繁体   English

Angular 8 替换动态插入内容中的标签

[英]Angular 8 replacing tags in dynamically inserted content

Is it possible to replace dynamically inserted angular component tags?是否可以替换动态插入的 angular 组件标签?

I have text like the following which I am getting from an api:我从 api 得到如下文本:

Testing <app-test></app-test> for <app-test></app-test> dynamically inserting another <app-test></app-test>

I have a test component and all I am trying to achieve is for the output from the text component to replace the <app-test></app-test> tags.我有一个测试组件,我想要实现的只是使用文本组件中的 output 替换<app-test></app-test>标记。

In my ts file, I am using DomSanitizer so my tags are not striped out, shown below:在我的 ts 文件中,我使用的是 DomSanitizer,因此我的标签不会被剥离,如下所示:

this.text = this.sanitizer.bypassSecurityTrustHtml("Testing <app-test></app-test> for <app-test></app-test> dynamically inserting another <app-test></app-test>");

And in my html I am using one-way binding to display the text, like below:在我的 html 中,我使用单向绑定来显示文本,如下所示:

<div [innerHTML]='text'></div>

But the output is just the text without the tags.但是 output 只是没有标签的文本。

You can create a dynamic components by a component ref In this link you can read about it: https://medium.com/@lukaonik/how-to-create-dynamic-components-in-angular-a2f449acb987您可以通过组件引用创建动态组件在此链接中您可以阅读有关它的信息: https://medium.com/@lukaonik/how-to-create-dynamic-components-in-angular-a2f449acb987

Other solution but not the best is call different components by an ng-swich其他解决方案但不是最好的解决方案是通过 ng-swich 调用不同的组件

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

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