如何使用$ sce.trustAsHtml(字符串)在Angular 1.2+中复制ng-bind-html-unsafe

tim*_*aak 224 angularjs

ng-bind-html-unsafe 已在Angular 1.2中删除

我正在尝试实现我需要使用的东西ng-bind-html-unsafe.在文档和github提交中,他们说:

当绑定到$ sce.trustAsHtml(string)的结果时,ng-bind-html提供ng-html-bind-unsafe like行为(innerHTML是没有清理的结果).

你怎么做到这一点?

Chr*_*ris 629

过滤

app.filter('unsafe', function($sce) { return $sce.trustAsHtml; });
Run Code Online (Sandbox Code Playgroud)

用法

<ANY ng-bind-html="value | unsafe"></ANY>
Run Code Online (Sandbox Code Playgroud)

  • 比接受的答案更好的解决方案,谢谢! (33认同)
  • 版本1.2中的@felix(当他们添加这个时)它默认启用为核心的一部分,而不是`ngSanitize`,因此不需要`ngSanitize` (9认同)
  • @OliverJosephAsh因为$ sce服务是在ngSanitize中定义的.它们破坏了主要功能,因此我们可以稍微使用角度,而不必总是使用整个框架. (2认同)
  • 这是由角度团队做出的设计决策 - 应该如何实现过滤器 - 如果你这样做,它们将无法工作.这必须返回一个函数的原因是,角度可以延迟处理,直到它"找到正确的时刻".否则,框架对调用过滤器的时间没有任何影响.这既好又坏,但据我所知 - 有必要应对角度的棘手处理.更多信息:http://docs.angularjs.org/api/ng/provider/$filterProvider (2认同)

Nen*_*nad 241

那应该是:

<div ng-bind-html="trustedHtml"></div>
Run Code Online (Sandbox Code Playgroud)

加上你的控制器:

$scope.html = '<ul><li>render me please</li></ul>';
$scope.trustedHtml = $sce.trustAsHtml($scope.html);
Run Code Online (Sandbox Code Playgroud)

而不是旧语法,您可以$scope.html直接引用变量:

<div ng-bind-html-unsafe="html"></div>
Run Code Online (Sandbox Code Playgroud)

正如几位评论者指出的那样,$sce必须在控制器中注入,否则会$sce undefined出错.

 var myApp = angular.module('myApp',[]);

 myApp.controller('MyController', ['$sce', function($sce) {
    // ... [your code]
 }]);
Run Code Online (Sandbox Code Playgroud)

  • 另外值得一提的是$ sce需要传递到控制器或者你得到$ sce没有定义 (25认同)
  • 如何使用函数返回的值执行此操作?<p ng-bind-html =""> {{description(category.id)}} </ p> (10认同)
  • 不确定我是否说得对,但是:`<p ng-bind-html ="trustedHtml"> </ p>`和`$ scope.trustedHtml = $ sce.trustAsHtml(description(category.id));; (2认同)
  • 但是...... var x = sce.trustAsHtml('foo'); var y = sce.trustAsHtml('foo'); X ==ÿ; false ...所以不应该创建一个无限的摘要循环,因为你的函数返回一个新对象? (2认同)

Mic*_*ins 16

我个人在进入数据库之前使用一些PHP库清理所有数据,因此不需要为我提供另一个XSS过滤器.

来自AngularJS 1.0.8

directives.directive('ngBindHtmlUnsafe', [function() {
    return function(scope, element, attr) {
        element.addClass('ng-binding').data('$binding', attr.ngBindHtmlUnsafe);
        scope.$watch(attr.ngBindHtmlUnsafe, function ngBindHtmlUnsafeWatchAction(value) {
            element.html(value || '');
        });
    }
}]);
Run Code Online (Sandbox Code Playgroud)

使用:

<div ng-bind-html-unsafe="group.description"></div>
Run Code Online (Sandbox Code Playgroud)

要禁用$sce:

app.config(['$sceProvider', function($sceProvider) {
    $sceProvider.enabled(false);
}]);
Run Code Online (Sandbox Code Playgroud)


ngu*_*yên 8

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

1.使用过滤器

app.filter('unsafe', function($sce) { return $sce.trustAsHtml; });
Run Code Online (Sandbox Code Playgroud)

使用(html):

<span ng-bind-html="line | unsafe"></span>
==>click `aaa` show alert box
Run Code Online (Sandbox Code Playgroud)

2.使用ngSanitize:更安全

包括 angular-sanitize.js

<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
Run Code Online (Sandbox Code Playgroud)

添加ngSanitizeroot角应用程序

var app = angular.module("app", ["ngSanitize"]);
Run Code Online (Sandbox Code Playgroud)

使用(html):

<span ng-bind-html="line"></span>
==>click `aaa` nothing happen
Run Code Online (Sandbox Code Playgroud)


STE*_*EEL 6

只需创建过滤器就可以解决问题。(为Angular 1.6提供了答案)

.filter('trustHtml', [
        '$sce',
        function($sce) {
            return function(value) {
                return $sce.trustAs('html', value);
            }
        }
    ]);
Run Code Online (Sandbox Code Playgroud)

并在html中使用它,如下所示。

<h2 ng-bind-html="someScopeValue | trustHtml"></h2>
Run Code Online (Sandbox Code Playgroud)