ng-bind-html doesn't work properly

25,514

Solution 1

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

Solution 2

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>

Solution 3

<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"></span></div>            
   </div>

If you bind to a value with HTML in it, you should use ngBindHtml.These bindings {{ foo }} prevent injecting actual HTML for security reasons.

Also check this $sce

EDIT

Install angular-sanitize and include it in your dependencies...

Angular sanitize / ng-bind-html not working?

Share:
25,514
Merrizee
Author by

Merrizee

Updated on July 31, 2022

Comments

  • Merrizee
    Merrizee almost 2 years

    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?

  • paul
    paul almost 10 years
    ng-bind-html-unsafe has been deprecated
  • Srinath
    Srinath almost 10 years
    can you create a fiddle of what you did...?