ng-bind-html vs bind-html-compile?

人走茶凉 提交于 2019-12-08 22:31:24

问题


I want to know the difference between ng-bind-html and bind-html-compile directives. For example I gave

<p style='color:red'>test<p> 

to ng-bind-html, this strips out the style where as bind-html-compile does not. May I know when each directive should be used. Thanks.


回答1:


bind-html-compile is not a standard Angular directive, it comes with the module https://github.com/incuna/angular-bind-html-compile and it is used to compile binded data.To make it simple, it is equivalent to write html in your source code: it will be re-evaluated and if other directives are found, they will work as expected.

ng-bind-html is a standard directive (bundled with Angular itself) and just output html strings without compiling it.

for example, if you controller has a variable with plain html, like in:

$scope.dataToDisplay = '<h1><strong>Title</strong></h1>';

Then you can go with ng-bind-html.

If you need to inject variables that contain html with other directives, such as:

$scope.dataToDisplay = '<h1 ng-show="showIfOtherVariable"><strong>Title</strong></h1>';

then you need to aforementioned module.



来源:https://stackoverflow.com/questions/39986720/ng-bind-html-vs-bind-html-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!