AngularJS - ngBindHtml and 'unsafe' html

前端 未结 2 434
不知归路
不知归路 2021-01-16 18:26

I am trying to render some unsafe HTML (basically a HTML snippet with some inline styling) and have the following code in my view:

相关标签:
2条回答
  • 2021-01-16 19:00

    The ng-bind-html-escape was removed in AngularJs 1.2.

    To achieve the same effect I would advise you to create a filter to trust the resource (you should include the $sce module ):

    app.filter('unsafe', function($sce) {
        return function(val) {
            return $sce.trustAsHtml(val);
        }; });
    

    Usage:

    <ELEMENT ng-bind-html="htmlValue | unsafe"></ELEMENT>
    

    You shouldn't forget to include the ngSanitize as the app dependency:

    angular.module('app', ['ngSanitize'])
    

    Cheers.

    0 讨论(0)
  • 2021-01-16 19:26

    You can bypass it using $sce.trustAsHtml . See documentation

    self.snippet.content = $sce.trustAsHtml('some html');
    
    0 讨论(0)
提交回复
热议问题