adding dynamic image to table column does not work in angularjs

自作多情 提交于 2019-12-20 01:01:50

问题


I'm new to angulajs and I'm trying to dynamically add image to a column when user selects it. Here is the below code and it did not work. code in .cshtml file ---

<table>
        <tr>
            <td>
                <img ng-src="{{path}}" />
                <input type="file" ng-model="path" />
            </td>
        </tr>
</table>

Code in angularjs file

$scope.path = "";

I came across 1 example but written in jquery and this what exactly i need it. http://jsfiddle.net/dwebexperts/4FGg8/1/

Thanks


回答1:


For your example, which looks quiet straight forward. I would recommend not to get into any plugin. Check this plukr

  $scope.imageUpload = function(event){
   var files = event.target.files; 
   var reader = new FileReader();
   reader.onload = $scope.imageIsLoaded; 
   reader.readAsDataURL(files[0]);
  }

  $scope.imageIsLoaded = function(e){
   $scope.$apply(function() {
      $scope.path = e.target.result;
   });
  }

and in your html

<input type="file" ng-model="path" onchange="angular.element(this).scope().imageUpload(event)"/>

NOTE: : ng-change is not supported for type file



来源:https://stackoverflow.com/questions/49768049/adding-dynamic-image-to-table-column-does-not-work-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!