Angularjs ng-bind-html-unsafe replacement

后端 未结 4 1332
梦毁少年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

    Simpler again.

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

    Usage:

    <any ng-bind-html="content | unsafe"></any>

    For more on html binding check the docs here.

    Just a warning: make sure you actually trust the html, or you could be opening a hole in your sites security.

    0 讨论(0)
  • 2020-12-09 13:15

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

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

    <div ng-app="ngBindHtmlExample">
      <div ng-controller="ngBindHtmlCtrl">
       <p ng-bind-html="myHTML" compile-template></p>
      </div>
    </div>
    
    
    
    var app = angular.module('app', []);
    
    app.controller('testApp', function( $scope ) {
        $scope.testHTML = '<h1> Welcome :) </h1>';
    });
    
    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);
            });
    
        };
    });
    
    0 讨论(0)
  • 2020-12-09 13:28

    Well, it's quite simple to just create your own directive, here is an example.

    Directive:

    app.directive('bindHtmlUnsafe', function( $compile ) {
        return function( $scope, $element, $attrs ) {
    
            var compile = function( newHTML ) { // Create re-useable compile function
                newHTML = $compile(newHTML)($scope); // Compile html
                $element.html('').append(newHTML); // Clear and append it
            };
    
            var htmlName = $attrs.bindHtmlUnsafe; // Get the name of the variable 
                                                  // Where the HTML is stored
    
            $scope.$watch(htmlName, function( newHTML ) { // Watch for changes to 
                                                          // the HTML
                if(!newHTML) return;
                compile(newHTML);   // Compile it
            });
    
        };
    });
    

    Usage:

    <div bind-html-unsafe="testHTML"></div>
    

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

    0 讨论(0)
  • 2020-12-09 13:28

    Simplest way, without $sce:

    module.directive('html', function() {
        function link(scope, element, attrs) {
    
            var update = function() {
                element.html(scope.html);
            }
    
            attrs.$observe('html', function(value) {
                update();
            });
        }
    
        return {
            link: link,
            scope:  {
                html:   '='
            }
        };
    });
    

    How to use:

    <div html="angular.variable"></div>
    
    0 讨论(0)
提交回复
热议问题