你如何使用 $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+
问题描述
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屋!