0

尝试解决 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

4

1 回答 1

2

自己找到了答案

app.config( ['$compileProvider', function($compileProvider){   
  $compileProvider.imgSrcSanitizationWhitelist(/^\s*(blob):/);
}]);
于 2013-11-20T14:10:49.113 回答