问题
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