angularjs – 你如何使用$ sce.trustAsHtml(string)来复制ng-bind-html-unsafe在Angular 1.2

前端之家收集整理的这篇文章主要介绍了angularjs – 你如何使用$ sce.trustAsHtml(string)来复制ng-bind-html-unsafe在Angular 1.2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
ng-bind-html-unsafe在Angular 1.2中被删除

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

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).

你怎么做到这一点?

应该是:
<div ng-bind-html="trustedHtml"></div>

加上你的控制器:

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

而不是旧的语​​法,在那里你可以直接引用$ scope.html变量:

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

正如几个评论者指出的,$ sce必须在控制器中注入,否则你会得到$ sce未定义的错误

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

 myApp.controller('MyController'['$sce',function($sce) {
    // ... [your code]
 }]);
原文链接:https://www.f2er.com/angularjs/147774.html

猜你在找的Angularjs相关文章