ng-bind-html-unsafe在Angular 1.2中被移除

我试图实现的东西,我需要使用ng-bind-html-不安全。在文档和github提交中,他们说:

ng-bind-html提供了ng-html-bind-不安全的行为(innerHTML的结果没有 当绑定到$sce.trustAsHtml(string)的结果时,sanitization)。

怎么做呢?


当前回答

var line = “<label onclick=”alert(1)“>aaa”</label>;

1. 使用过滤器

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

使用(html):

<span ng-bind-html="line | unsafe"></span>
==>click `aaa` show alert box

2. 使用ngSanitize:更安全

包括angular-sanitize.js

<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>

在angular的根应用中添加ngSanitize

var app = angular.module("app", ["ngSanitize"]);

使用(html):

<span ng-bind-html="line"></span>
==>click `aaa` nothing happen

其他回答

JavaScript

$scope.get_pre = function(x) {
    return $sce.trustAsHtml(x);
};

HTML

<pre ng-bind-html="get_pre(html)"></pre>
$scope.trustAsHtml=function(scope)
{
    return $sce.trustAsHtml(scope);
}
<p class="card-text w-100" ng-bind-html="trustAsHtml(note.redoq_csd_product_lead_note)"></p>

如果你想要回到旧的指令,你可以添加这个到你的应用程序:

指令:

directives.directive('ngBindHtmlUnsafe', ['$sce', function($sce){
    return {
        scope: {
            ngBindHtmlUnsafe: '=',
        },
        template: "<div ng-bind-html='trustedHtml'></div>",
        link: function($scope, iElm, iAttrs, controller) {
            $scope.updateView = function() {
                $scope.trustedHtml = $sce.trustAsHtml($scope.ngBindHtmlUnsafe);
            }

            $scope.$watch('ngBindHtmlUnsafe', function(newVal, oldVal) {
                $scope.updateView(newVal);
            });
        }
    };
}]);

使用

<div ng-bind-html-unsafe="group.description"></div>

来源:https://github.com/angular-ui/bootstrap/issues/813

过滤器

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

使用

<ANY ng-bind-html="value | unsafe"></ANY>

var line = “<label onclick=”alert(1)“>aaa”</label>;

1. 使用过滤器

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

使用(html):

<span ng-bind-html="line | unsafe"></span>
==>click `aaa` show alert box

2. 使用ngSanitize:更安全

包括angular-sanitize.js

<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>

在angular的根应用中添加ngSanitize

var app = angular.module("app", ["ngSanitize"]);

使用(html):

<span ng-bind-html="line"></span>
==>click `aaa` nothing happen