如何通过评估角度表达式来修改innerHTML [英] How to modify innerHTML by evaluating an angular expression

查看:21
本文介绍了如何通过评估角度表达式来修改innerHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做这样的事情

{{item.innerHTML}}

item.innerHTML 包含需要去那里的 html,但由于它是 dom 的一部分,所以它被替换为一个字符串.有没有办法让它取代innerHTML?

谢谢!

解决方案

你需要使用 ngBindHtml :

http://plnkr.co/edit/n1rLzgEZQoa2tJf0qnVZ?p=preview

在控制器中:

$scope.content = "这是粗体内容";

html:

<div ng-bind-html="content"></div>

您将需要以下模块:

http://code.angularjs.org/1.0.3/angular-消毒.js

确保将 ngSanitize 声明为模块依赖项,例如:

var app = angular.module('angularjs-starter', ["ngSanitize"]);

I am trying to do something like this

<div id="{{item.id}}" ng-repeat="item in itemList">
    {{item.innerHTML}}
</div>

item.innerHTML contains the html that needs to go there, but since it is part of the dom, it is replaced as a string. Is there a way to just have it replace the innerHTML?

Thanks!

解决方案

You need to use ngBindHtml :

http://plnkr.co/edit/n1rLzgEZQoa2tJf0qnVZ?p=preview

in the controller :

$scope.content = "<b>this is bold content</b>";

html :

<div ng-bind-html="content"></div>

you'll need the following module :

http://code.angularjs.org/1.0.3/angular-sanitize.js

Be sure to declare ngSanitize as a module dependancy, for instance :

var app = angular.module('angularjs-starter', ["ngSanitize"]);

这篇关于如何通过评估角度表达式来修改innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆