ng-bind-html doesn't work properly

cagin picture cagin · Jul 8, 2014 · Viewed 25k times · Source

I want to show my data as html in angularjs. Here is apart of my codes :

<div class="panel-body" ng-controller="hosgeldinizController">
    <div id="divHosgeldiniz" name="hosgeldinizMessages" ng-repeat="hosgeldinizMessage in hosgeldinizMessages">

        <div>
            <span ng-class-odd="'degisimIcerik'" ng-class-even="'degisimIcerik alternate'" ng-bind-html="hosgeldinizMessage.M_Icerik">{{hosgeldinizMessage.M_Icerik}} </span>
        </div>            
    </div>
</div>

But it doesnt show as html it shows just like normal text however hosgeldinizMessage.M_Icerik contains html elements. What should I do to show as html?

Answer

cagin picture cagin · Jul 8, 2014

It worked for me

In controller...

  $scope.trustedHtml = function (plainText) {
            return $sce.trustAsHtml(plainText);
        }

In the html

   <span ng-class-odd="'degisimIcerik'" ng-class-even="'degisimIcerik alternate'" ng-bind-html="trustedHtml(hosgeldinizMessage.M_Icerik)"></span>