你如何使用 $sce.trustAsHtml(string) 在 Angular 1.2+ 中复制 ng-bind-html-unsafe [英] How do you use $sce.trustAsHtml(string) to replicate ng-bind-html-unsafe in Angular 1.2+

查看:25
本文介绍了你如何使用 $sce.trustAsHtml(string) 在 Angular 1.2+ 中复制 ng-bind-html-unsafe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<块引用>

ng-bind-html 提供类似 ng-html-bind-unsafe 的行为(innerHTML 的结果没有sanitization) 当绑定到 $sce.trustAsHtml(string) 的结果时.

你是怎么做到的?

解决方案

那应该是:

在您的控制器中加上:

$scope.html = '<ul><li>请渲染我</li></ul>';$scope.trustedHtml = $sce.trustAsHtml($scope.html);

代替旧语法,您可以直接引用 $scope.html 变量:

正如一些评论者指出的,$sce 必须被注入到控制器中,否则你会得到 $sce undefined 错误.

 var myApp = angular.module('myApp',[]);myApp.controller('MyController', ['$sce', function($sce) {//... [你的代码]}]);

ng-bind-html-unsafe was removed in Angular 1.2

I'm trying to implement something where I need to use ng-bind-html-unsafe. In the docs and on the github commit they say:

ng-bind-html provides ng-html-bind-unsafe like behavior (innerHTML's the result without sanitization) when bound to the result of $sce.trustAsHtml(string).

How do you do this?

解决方案

That should be:

<div ng-bind-html="trustedHtml"></div>

plus in your controller:

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

instead of old syntax, where you could reference $scope.html variable directly:

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

As several commenters pointed out, $sce has to be injected in the controller, otherwise you will get $sce undefined error.

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

 myApp.controller('MyController', ['$sce', function($sce) {
    // ... [your code]
 }]);

这篇关于你如何使用 $sce.trustAsHtml(string) 在 Angular 1.2+ 中复制 ng-bind-html-unsafe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆