微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何让angular2 [innerHtml]工作

参见英文答案 > Angular HTML binding15个
因为没有错误报告,我不知道出了什么问题.

我有一个组件类

import { Component,OnInit,ViewContainerRef } from '@angular/core';

@Component({
  selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
 testhtml = "<p>Hello world</p>";
    constructor(){}

 }

}

在我的模板文件中,我做了类似这样的事情:

<div class="blog-post">[innerHtml]="testhtml"</div>

但这似乎不起作用.还有其他我需要导入的东西吗?

我正在使用angular-cli“版本”:“1.0.0-beta.26”,

Angular使用{{property}}来插值.这就是你在div中显示纯文本的方式,就像这样

解决方案1:

<div class="blog-post">{{testhtml}}</div>

但那会写出文字,而不是HTML.

对于HTML,您需要绑定到该属性

解决方案2:

<div class="blog-post" [innerHtml]="testhtml"></div>

注意我将[innerHtml]移动到div标签内.

省略方括号将绑定到属性,因此您需要再次插值

解决方案3:

<div class="blog-post" innerHtml="{{testhtml}}"></div>

属性绑定(解决方案2)是首选方法.

原文地址:https://www.jb51.cc/angularjs/143645.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐