Shafaet
Shafaet

Reputation: 435

Add text field using ng-bind-html

I am trying to create a text field inside a "p" tag using ng-bind-html. 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.

(I know I can put the text field directly in the html, but I need this for some reason)

Upvotes: 0

Views: 1195

Answers (1)

gkalpak
gkalpak

Reputation: 48211

It is because ngSanitize considers <input> to be unsafe. 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.

Upvotes: 2

Related Questions