Angular URL' trustAsResourceUrl'不工作

时间:2014-07-23 13:20:36

标签: javascript angularjs

我正在使用angular-file-upload.js并上传图片。上传成功后,它会返回托管图像的URL,该图像在Azure上作为blob托管。 上传成功,并且正确返回了URL,但是当我在“images”堆栈上推送此URL以在浏览器中查看此图像时,它将无法正常工作。例如,我得到的URL如下所示:

"https://searlesmedia.blob.core.windows.net/event-images/4_slide-archive.jpg"

Controller.js

uploader.onSuccessItem = function (fileItem, response, status, headers) {
    console.info('onSuccessItem', fileItem, response, status, headers);
    $scope.uploadcomplete = "Upload Successful";
    var url = $sce.trustAsResourceUrl(response);
    $scope.images.push(url);
};

HTML

<div class="row" data-ng-repeat="image in images">
    <img ng-src="{{image}}" class="img-responsive" />
</div>

1 个答案:

答案 0 :(得分:7)

嗨,好像你错过了什么,请看这里:http://plnkr.co/edit/1PpscI4dOMYpYRf6fbUS?p=preview

angular.module('mySceApp', ['ngSanitize'])
  .controller('AppController', ['$http', '$scope', '$sce',
    function($http, $scope, $sce) {

      $scope.image = {};
      $http.get("test_data.json").success(function(data) {
        console.log(data.url);
        // $scope.userComments = userComments;

        $scope.image.url = $sce.trustAsResourceUrl(data.url);
      });

    }
  ]);

HTML:

<!doctype html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Example - example-example64-production</title>


  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.16/angular.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.16/angular-sanitize.js"></script>
  <script src="script.js"></script>



</head>

<body ng-app="mySceApp">
  <div ng-controller="AppController as myCtrl">



    <div class="well">

      <b>{{userComments.name}}</b>:

      <img ng-src="{{image.url}}">
      <br>

    </div>
  </div>
</body>

</html>