javascript ng-bind-html 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12786345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:07:09  来源:igfitidea点击:

ng-bind-html does not work

javascriptangularjsjavascript-databinding

提问by Aladdin Mhemed

please look at thisfiddle

请看这个小提琴

please type 4 in the text area, it should write an error message, that is in this line:

请在文本区域输入 4,它应该写一条错误信息,即在这一行:

   $names.push('<div ng-bind-html-unsafe="snippet"></div>');

actually it does not. It prints the html code itself without handling,

实际上它没有。它在不处理的情况下打印 html 代码本身,

and the underlying html is:

底层的html是:

<li ng-repeat="user in list|splitList:appUsers" class="ng-scope ng-binding">
        &lt;div ng-bind-html="snippet"&gt;&lt;/div&gt;
    </li>

I tried to follow solutions from this topic

我试图遵循这个主题的解决方案

but nothing seem to help,

但似乎没有任何帮助,

any help in fixing it will be regarded,

任何修复它的帮助都将被视为,

回答by Aladdin Mhemed

I got help from the chat room of angularjs,

我从angularjs的聊天室得到了帮助,

the thing is that I should use:

问题是我应该使用:

<div ng-bind-html-unsafe="user">...</div>

instead of {{user}}

代替 {{user}}

回答by Eldad

To get it to work you will need the following:

要使其正常工作,您将需要以下内容:

Declare it in html

在 html 中声明

div ng-bind-html="{expression}"></div> 

Or

或者

div class="ng-bind-html: {expression};"></div>

AND

reference angular-sanitize.min.jsand then add

引用angular-sanitize.min.js然后添加

angular.module('YOUR_APP_NAME', ['ngSanitize']);

The last two are in the angularjs documentation for some reason...

由于某种原因,最后两个在 angularjs 文档中......