简体   繁体   English

如何在我的模板(Angular2 / TypeScript)中将HTML字符串转换为HTML?

[英]How can I convert strings of HTML into HTML in my template (Angular2/TypeScript)?

I have this object in a dummy-data.ts file. 我在dummy-data.ts文件中有这个对象。 Through a service I pull it successfully in to app.component.ts . 通过服务我成功将其app.component.ts

{name:"Object1", 
    prop1: {key:'value', key:'value'},
    password: "P@ssword1",
    htmlText:'<h4>This is THE demo text</h4><p>I want it to display as HTML</p>'
}

Currently app.component.ts looks like this, to start out simply: 目前app.component.ts看起来像这样,简单地开始:

@Component({
    selector: 'my-app',
    template: `<h1>{{title}}</h1>
        <p *ngFor="#plot of plots">
            {{plot.personalPanelText.transition}}
        </p>
`,
providers: [PlotService]  
})

The end result is strings instead of HTML. 最终结果是字符串而不是HTML。 I want the HTML to be code, not text with HTML tags. 我希望HTML是代码,而不是带HTML标签的文本。

I've tried a couple ways and poked around the world wide nets and Angular2 docs, but couldn't find a solution. 我尝试了几种方法,并在全球网和Angular2文档中搜索,但找不到解决方案。

Thanks in advance! 提前致谢!

You can bind to innerHtml like: 您可以绑定到innerHtml如:

<div [innerHtml]="someProp.htmlText"></div>

Angular does not instantiate any components where the selectors that match tags or attributes in HTML added this way nor will it resolve any bindings (like {{xx}} ) Angular不会实例化任何与HTML中的标记或属性匹配的选择器以这种方式添加的组件,也不会解析任何绑定(例如{{xx}}

See also How to bind raw html in Angular2 另请参见如何在Angular2中绑定原始html

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

相关问题 Angular2-如何从打字稿中获取变量到我的HTML dom - Angular2 - how to get a variable from typescript to my html dom 如何将String []传递给我的angular2 html模板 - How to pass a String[] to my angular2 html template 如何在Angular2中的Template指令中使用html? - How do I use html in Template directive in Angular2? 如何在angular2模板中使用Electron的webview html元素? - How can I use Electron's webview html element inside an angular2 template? 我该怎么用 <html> Angular2中的模板? - How can I use <html> templates in Angular2? 如何从Typescript文件附加HTML并在angular2中对其执行功能 - How to append html from typescript file and perform functionality on it in angular2 对于Angular2项目,我如何连接从typescript生成的所有JavaScript文件并将它们添加到我的index.html文件中 - For Angular2 project, In gulp how do I concat all my JavaScript files that were generated from typescript and add them to my index.html file 如何在Angular2中的模板html上动态创建数组组件? - How to rendered dynamically created array component on template html in Angular2? 如何将带有HTML实体的字符串转换为规范化形式? - How can I convert strings with HTML entities into normalized form? 如何在typescript中导入template.html - how can import template.html in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM