Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify innerHTML by evaluating an angular expression

Tags:

angularjs

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!

like image 648
pkrish Avatar asked Jan 09 '13 17:01

pkrish


1 Answers

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"]);
like image 51
mpm Avatar answered Oct 04 '22 04:10

mpm