ng-bind-html doesn't work properly

邮差的信 提交于 2019-12-07 07:03:23

问题


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?


回答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




回答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>



回答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?




回答4:


This line:

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

should be

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

without the {{}} expression.

Display of the content is already taken care by the ng-bind-html directive.




回答5:


I think using ng-bind-html-unsafe will get you what you need.

<div ng-repeat="hosgeldinizMessage in hosgeldinizMessages" ng-bind-html-unsafe="hosgeldinizMessage.M_Icerik"></div>

Here's a working fiddle: http://jsfiddle.net/nfreitas/aHfAp/

Documentation for the directive can be found here: http://docs.angularjs.org/api/ng.directive:ngBindHtmlUnsafe



来源:https://stackoverflow.com/questions/24628126/ng-bind-html-doesnt-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!