5

我想选择一个文件并将其加载到角度 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);
            };

  }
4

1 回答 1

11

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

于 2013-05-19T15:04:55.957 回答