简体   繁体   English

使用ng-bind-html添加文本字段

[英]Add text field using ng-bind-html

I am trying to create a text field inside a "p" tag using ng-bind-html. 我正在尝试使用ng-bind-html在“ p”标签内创建一个文本字段。 It is not not rendering input tags, but rendering other tags. 它不是渲染输入标签,而是渲染其他标签。

<!doctype html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-sanitize.min.js"></script>

</head>

<body>
<div ng-app="ss" ng-controller="formController">
<p ng-bind-html="hh"></p>

</div>

<script>
var myApp = angular.module("ss", ['ngSanitize']);
function formController ($scope) {
    $scope.hh="<b>Hello</b><input type='text'> ";
}
</script>

</body>
</html>

In the output I am getting the bold text "Hello", but not the input field. 在输出中,我得到的是粗体字“ Hello”,但没有输入字段。

(I know I can put the text field directly in the html, but I need this for some reason) (我知道我可以将文本字段直接放在html中,但是出于某些原因,我需要这样做)

It is because ngSanitize considers <input> to be unsafe. 这是因为ngSanitize认为<input>是不安全的。 If you want to do it anyway, you must explcitly trust it: 如果仍然要执行此操作,则必须明确信任它:

.controller('formController', function ($sce, $scope) {
    $scope.hh = $sce.trustAsHtml('<b>Hello</b><input type="text" />');
}

See, also, this short demo . 另请参见此简短演示


BTW, you should properly register your controllers (using .controller() ), because looking them up in the global object has been deprecated in the latest versions of Angular. 顺便说一句,您应该正确地注册您的控制器(使用.controller() ),因为在Angular的最新版本中不建议在全局对象中查找它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM