Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-bind-html doesn't work properly

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?

like image 598
cagin Avatar asked Dec 03 '22 19:12

cagin


2 Answers

Get ngSanitize as a dependancy in your module

    var app = angular.module("dene",['ngSanitize']);
    app.controller("deneCtrl",function(){
       this.selam = "<p>Merhaba <strong>Angular</strong></p>";
    });

and use ng-bind-html in your view

<div ng-controller="deneCtrl as dene">
<span ng-bind-html = " dene.selam "> </span>
</div>

but be sure to include the related versions

as in

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-sanitize.min.js"></script>

or if you prefer it locally you download a copy https://code.angularjs.org/

point to note

Make sure that you have the same version of ngSanitize as AngularJS version you are running ,

well .... for some reason if you mixed them up (e.g angular 1.2.23 and angular-sanitize 1.0.0) you will end up with either a sanitized version in your view (it will work) but ALOT of ERRORS in your console or sometimes it wont render on screen at ALL :) - I believe that's what you are facing

Trust me , I 've been there :D

like image 161
Fahad Avatar answered Dec 23 '22 16:12

Fahad


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>
like image 21
cagin Avatar answered Dec 23 '22 16:12

cagin