I'm trying to show a photo when someone selects it for an input. Now I might be over simplyfying angularjs too much but I was hoping that this code would work.
<div ng-app="app">
... html stuff ...
<div ng-controller="imgCtrl">
<input ng-model="imageSource" type="file"></input>
<img ng-src="{|imageSource|}"></img>
</div>
</div> end of app
Where my angularjs file is as follows
var app = angular.module('app',[]).config(function($interpolateProvider){
$interpolateProvider.startSymbol('{|');
$interpolateProvider.endSymbol('|}');
}
);
var imgCtrl = function ($scope,$http){
$scope.wall = _wall;
};
clientDashboard.controller('imgCtrl',imgCtrl);
Unfortunately, nothing is happening and I can't see my updated image. Do I really have to write boiler plate code for this?
I got it working in chrome and firefox but my quick research shows that it is a security risk and may not even be allowed eventually. See here: http://jsfiddle.net/28ZJw/
HTML:
<div ng-app="app">
<div ng-controller="imgCtrl">
<input ng-model="imageSource" type="file" onchange="angular.element(this).scope().fileNameChaged(this)"></input>
<img ng-src="{|imageSource|}"></img>
</div>
</div>
JS:
var app = angular.module('app',[]).config(function($interpolateProvider){
$interpolateProvider.startSymbol('{|');
$interpolateProvider.endSymbol('|}');
}
);
app.controller('imgCtrl', function($scope, $http)
{
$scope.imageSource = "";
$scope.fileNameChaged = function(element)
{
var reader = new FileReader();
reader.onload = function(e)
{
$scope.$apply(function()
{
$scope.imageSource = e.target.result;
});
}
reader.readAsDataURL(element.files[0]);
}
});
The right way would be to get this going with ng-change however I couldn't get it to work. I am not sure if ng-change applies to file input fields
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With