html - 将 Html 与 Angular 中的变量绑定
问题描述
我需要从后面渲染一些 HTML。像这样的东西:
<strong> Item label: {{item.label}}</strong>
我尝试这样做:
<div [innerHTML]="html"></div>
但它的渲染:
Item label: {{item.label}}
但我有项目对象
item = {
label : 'Label from item' }
我需要渲染
Item label: Label from item
我创建一个例子:
解决方案
使用 ES6 字符串插值
@Component({
selector: 'app-component',
template: '<div [innerHtml]="html" ></div>',
styleUrls: [ './app.component.scss' ]
})
export class AppComponent {
item = {
label: 'Label from item'
};
html: any = `<strong> Item label: ${this.item.label}</strong>`;
constructor() { }
}
模板字符串使用反引号 (``) 而不是单引号或双引号。
模板字符串可以使用占位符使用 ${ } 语法进行字符串替换。
推荐阅读
- laravel - Laravel 条件雄辩查询
- amazon-s3 - 使用存储在 s3 中的 parquet 文件在 Greenplum 中创建外部表
- javascript - 循环查询选择器,未定义的内部循环
- arrays - 将 traceroute IP 与数组值进行比较
- android - 如何在 BookmarkActivity 上添加 Toast.makeText
- javascript - 添加和删除输入复选框的值
- xamarin - 如何限制 SKCanvasView 的平移和缩放?
- python - 根据 2 列从另一个表中添加缺失的行
- mysql - 如何在mysql中添加两个求和值
- html - HTML 最初是使用 SGML 实现的吗?