对于输入标签NG绑定,HTML不工作 [英] ng-bind-html doesnt work for Input tags

查看:215
本文介绍了对于输入标签NG绑定,HTML不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图存储可变范围内的HTML,然后在模板视图中使用它。当我在读如何在角做到这一点,我碰到 NG-绑定-HTML 。在我已经注意到,当我绑定&LT html标签;一> <强> 等.. 有用。但我无法添加<输入方式> 标记它

I am trying to store a HTML inside a scope variable and then use it in template view. When I was reading how to do this in angular, I came across ng-bind-html. In that I've noticed that when I bind html tags with <a>, <strong>, etc.. it works. But I am unable to add <input> tags to it.

含义,这个作品:

$scope.myHtml = '<strong>This is <a hreaf="#">Something</a></strong>';

模板:

<p ng-bind-html="myHtml"> </p>

但是,这并不工作:​​

$scope.myHtml = '<input type="text" />';

模板:

<p ng-bind-html="myHtml"> </p>

以上仅仅是仅用于演示目的的简化样本。我的问题是:

The above is just a simplified sample for demonstration purpose only. My question is:

1)标签是否不NG绑定,HTML指令工作?

2)如果没有,我怎么能html的绑定一个输入标签,所以我可以将其插入视图内?

推荐答案

您得到$ SCE:不安全的错误...

you are getting $sce:unsafe error...

这意味着你应该使用 NG-结合HTML的不安全但angularjs的较新版本不包含这个指令了,所以你768,16使用$ sce.trustAsHtml()这样的...

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" />');

但这种方式,你不能绑定范围内的变量到HTML所以最好的办法是写一个指令,它可与被取代NG绑定HTML的不安全 ...

在这里工作 PLUNKER 为$ SCE和指令的例子...

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

这篇关于对于输入标签NG绑定,HTML不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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