Angular(1.2)$ sce阻止我的blob src

时间:2013-11-06 22:12:10

标签: javascript angularjs

尝试解决ng-src信任问题

angular.module('App', []);

function Ctrl($scope, $sce) {

  // Construct a dummy blob Url 
  var canvas = document.createElement("canvas");
  canvas.width = 100;
  canvas.height = 100;

  var ctx = canvas.getContext("2d");

  ctx.fillStyle="#FF0000";
  ctx.fillRect(0,0,100,100);

  canvas.toBlob(function(blob){
    $scope.src = $sce.trustAsResourceUrl(URL.createObjectURL(blob));

    // works...
    var img = document.createElement("img");
    img.src = $scope.src;
    document.body.appendChild(img);
  });


}

http://plnkr.co/edit/DqRrzMv4YzIi0Hd1Zubu?p=preview

1 个答案:

答案 0 :(得分:2)

自己找到答案

app.config( ['$compileProvider', function($compileProvider){   
  $compileProvider.imgSrcSanitizationWhitelist(/^\s*(blob):/);
}]);