ng-bind-html doesnt work for Input tags

时光总嘲笑我的痴心妄想 提交于 2019-11-28 11:59:39

you are getting $sce:unsafe error...

this means you should use ng-bind-html-unsafe but newer version of angularjs does not include this directive anymore so you shoud use $sce.trustAsHtml() like this...

$scope.trustedInputHtml = $sce.trustAsHtml('<input type="text" />');

but this way you cannot bind scope variables to your html so best way is writing a directive which can be replace with ng-bind-html-unsafe...

here is working PLUNKER for both $sce and directive examples...

I would keep the stuff you insert in its own template and use ng-include (http://docs.angularjs.org/api/ng/directive/ngInclude).

Having a angular template (template.html) with the contents:

<strong>This is <a href="#">Something</a></strong>

You can include it with

<p ng-include="template.html"></p>

This results in sth like

<p ng-include="template.html"><span class="ng-scope"><strong>This is <a href="#">Something</a></strong></span></p>

Angular selectively sanitizes certain HTML tags with ng-bind-html

so if you want all the tags you should use ng-bind-html-unsafe instead

but watch out of XSS attacks !

BTW

It's far better to follow the @Ed Hinchliffe piece of advice

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