图像处理 – 如何在Angular JS中更改src后刷新图像

前端之家收集整理的这篇文章主要介绍了图像处理 – 如何在Angular JS中更改src后刷新图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想选择一个文件并加载角度js.

一切都很好,图像不会重新解决的唯一问题.我可以看到一切正常,因为当我用angular.js在我的页面上切换菜单时,图像已经刷新了.

这是我的代码

<div ng-controller="TopMenuCtrl">
        <button class="btn" ng-click="isCollapsed = !isCollapsed">Toggle collapse</button>
        <input ng-model="photo"
           onchange="angular.element(this).scope().file_changed(this)"
           type="file" accept="image/*" multiple />
            <output id="list"></output>
            <img ng-src="{{imageSource}}">
    </div>

和Angular js脚本:

$scope.file_changed = function(element) {
          var files = element.files; // FileList object
          // Loop through the FileList and render image files as thumbnails.
          for (var i = 0,photofile; photofile = files[i]; i++) { 

             // var photofile = element.files[0];
              var reader = new FileReader();
              reader.onload = (function(theFile) {
                    return function(e) {

                    $scope.imageSource= e.target.result;

                    };
                  })(photofile);

              reader.readAsDataURL(photofile);
            };

  }

解决方法

在onLoad函数中手动更新$scope.imageSource时,必须调用 Scope.$apply,因为Angular无法猜测何时进行此更改.

猜你在找的Angularjs相关文章