Angular2 - 用 html 插入字符串 [英] Angular2 - Interpolate string with html
问题描述
如何使用 angular2 用 html 插入字符串.我知道在 angular 1.x 中有 $interpolate(templateString)(miniScope);
但我没有找到相同的 angular2.
How to Interpolate string with html using angular2. i know in angular 1.x there is $interpolate(templateString)(miniScope);
but i haven't find the same for angular2.
假设我有一个这样的模板:你好,我的名字是{{name}}
suppose i have a template like this : Hello my name is {{name}}
和绑定就像 name: "Pardeep"
and binding is like name: "<strong>Pardeep</strong>"
所以我想要像
你好,我叫Pardeep
有什么帮助吗?
推荐答案
您可以简单地使用 [innerHTML]
指令来完成它.
You can simply use [innerHTML]
directive to accomplish it.
http://plnkr.co/edit/6x04QSKhqbDwPvdsLSL9?p=preview
import {Component, Pipe} from '@angular/core'
@Component({
selector: 'my-app',
template: `
Hello my name is <span [innerHTML]="myName"></span>
`,
})
export class AppComponent {
myName='<strong>Pardeep</strong>';
}
更新:
我在 RC.1 发布后检查它不能以这种方式工作.
I checked it doesn't work this way after RC.1 release.
假设要使其与 RC.4 一起使用,您可以使用 DomSanitizationService
如下所示,
Let's say to make it work with RC.4 you can use DomSanitizationService
as shown below,
@Component({
selector: 'my-app',
template: `
<div [innerHTML]="myCheckbox"></div>
`,
})
export class AppComponent {
dangerousUrl='<input type="checkbox">';
constructor(sanitizer: DomSanitizationService) {
this.myCheckbox= sanitizer.bypassSecurityTrustHtml(this.dangerousUrl);
}
}
http://plnkr.co/edit/Yexm1Mf8B3FRhNch3EMz?p=preview
这篇关于Angular2 - 用 html 插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!