Casey
Casey

Reputation: 3353

Inject HTML in Angular (>1.2) with input tags and other "unsafe" elements

I have some HTML I get in JSON which needs to be injected into a page. I used $sce.trustAsHtml and bound the output with ngBindHtml, but that strips out, apparently, tags like <select> and <input> which are "unsafe." Is there some way to tell Angular I really, actually did intend to insert exactly the HTML I asked for into the page?

Right now I just have something like $scope.$watch('response.htmlBody', function(){$('#container').html($scope.response.htmlBody);} and it seems to work as intended but that seems like a pretty lousy hack.

In this case the HTML is free of Angular-specific elements, so I do not need to worry about compiling it again.

Upvotes: 1

Views: 162

Answers (1)

New Dev
New Dev

Reputation: 49590

$sce.trustAsHtml and ng-bind-html should work without removing <input> and <select>:

$scope.html = $sce.trustAsHtml(data);

<div ng-bind-html="html"></div>

plunker

Upvotes: 1

Related Questions