亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用$sce.TrustAsHtml(String)復制ng-bind-html-角1.2+中

如何使用$sce.TrustAsHtml(String)復制ng-bind-html-角1.2+中

慕村225694 2019-06-20 17:15:48
如何使用$sce.TrustAsHtml(String)復制ng-bind-html-角1.2+中的不安全ng-bind-html-unsafe移除角1.2我在努力實現我需要使用的東西ng-bind-html-unsafe..在文檔和GitHub提交文件中,他們說:Ng-bind-html提供了ng-html-綁定-不安全的類似行為(innerHTML的結果沒有衛生化),當綁定到$sce.TrustAsHtml(String)的結果時。你怎么做到的?
查看完整描述

3 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

這應該是:

<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 undefined錯誤。

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

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


查看完整回答
反對 回復 2019-06-20
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

濾光器

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

使用

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


查看完整回答
反對 回復 2019-06-20
?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

就我個人而言,在進入數據庫之前,我使用一些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 || '');
        });
    }}]);

使用:

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

禁用$sce:

app.config(['$sceProvider', function($sceProvider) {
    $sceProvider.enabled(false);}]);


查看完整回答
反對 回復 2019-06-20
  • 3 回答
  • 0 關注
  • 1066 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號