Javascript 使用ng bind html添加文本字段
我正在尝试使用ng bind html在“p”标记内创建一个文本字段。它不是呈现输入标记,而是呈现其他标记Javascript 使用ng bind html添加文本字段,javascript,html,angularjs,Javascript,Html,Angularjs,我正在尝试使用ng bind html在“p”标记内创建一个文本字段。它不是呈现输入标记,而是呈现其他标记 <!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.googl
<!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>
var myApp=angular.module(“ss”,['ngSanitize']);
函数formController($scope){
$scope.hh=“你好”;
}
在输出中,我得到的是粗体文本“Hello”,而不是输入字段
(我知道我可以将文本字段直接放在html中,但出于某种原因我需要它)这是因为
ngSanitize
认为
不安全。
如果你无论如何都想这样做,你必须明确地相信它:
.controller('formController', function ($sce, $scope) {
$scope.hh = $sce.trustAsHtml('<b>Hello</b><input type="text" />');
}
.controller('formController',函数($sce,$scope){
$scope.hh=$sce.trustAsHtml('Hello');
}
另请参见此
顺便说一句,您应该正确注册控制器(使用
.controller()
),因为在全局对象中查找控制器在Angular的最新版本中已被弃用。