Angularjs ng-bind-html-unsafe replacement

后端 未结 4 1340
梦毁少年i
梦毁少年i 2020-12-09 12:50

I used to be able to use ng-bind-html-unsafe to output unsanitized code (because sanitization happens serverside).

But now that option is gone? I know I

4条回答
  •  旧巷少年郎
    2020-12-09 13:15

    I would strongly recommend checking out this SIMPLE JSFiddle example. Was a lifesaver:

    http://jsfiddle.net/cC5VZ/2/

    var app = angular.module('app', []); app.controller('testApp', function( $scope ) { $scope.testHTML = '

    Welcome :)

    '; }); app.directive('bindHtmlUnsafe', function( $parse, $compile ) { return function( $scope, $element, $attrs ) { var compile = function( newHTML ) { newHTML = $compile(newHTML)($scope); $element.html('').append(newHTML); }; var htmlName = $attrs.bindHtmlUnsafe; $scope.$watch(htmlName, function( newHTML ) { if(!newHTML) return; compile(newHTML); }); }; });

提交回复
热议问题