18

I am trying to do something like this

<div id="{{item.id}}" ng-repeat="item in itemList">
    {{item.innerHTML}}
</div>

item.innerHTML contains the html that needs to go there, but since it is part of the dom, it is replaced as a string. Is there a way to just have it replace the innerHTML?

Thanks!

1 Answer 1

36

You need to use ngBindHtml :

http://plnkr.co/edit/n1rLzgEZQoa2tJf0qnVZ?p=preview

in the controller :

$scope.content = "<b>this is bold content</b>";

html :

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

you'll need the following module :

http://code.angularjs.org/1.0.3/angular-sanitize.js

Be sure to declare ngSanitize as a module dependancy, for instance :

var app = angular.module('angularjs-starter', ["ngSanitize"]);
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this, but it does not seem to work. This is what I get in the html <div id="item1"> <div ng-bind-html="<p>item1 description</p>" class="ng-binding"></div> </div> . Am I missing something?
the exemple works as far as i know : plnkr.co/edit/n1rLzgEZQoa2tJf0qnVZ?p=preview you must have forgot to do something, also dont put {{}} in ng-bind-html since it is an expression.
Thanks! That worked. I thought you use {{}} to evaluate expressions? docs.angularjs.org/guide/expression
@mpm 's answer works! But just as a reminder, don't forget to include "angular-sanitize.js" in your application. And also as mpm mentioned, don't forget to declare module dependancy.
Angular 4: to pass HTML as variable you can use <tag [innerHTML]="passHTML"><tag>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.