admin管理员组文章数量:1405380
There is the following code:
handleFileSelect = (evt) ->
console.log(1)
file = evt.currentTarget.files[0]
reader = new FileReader()
reader.onload = (evt) ->
$scope.$apply ($scope) ->
$scope.myImage = evt.target.result
reader.readAsDataURL(file)
angular.element(document.querySelector('#fileInput')).on('change', handleFileSelect)
And HTML:
<div class="form-group ng-scope">
<label class="col-sm-2 control-label" for="image">Image:</label>
<div class="col-sm-10">
<div class="cropFile">
<input id="fileInput" type="file">
</div>
</div>
</div>
I want to get '1' in console after I change some file in #fileInput
. But when I choose file I get nothing. How can I fix it? Thanks!
There is the following code:
handleFileSelect = (evt) ->
console.log(1)
file = evt.currentTarget.files[0]
reader = new FileReader()
reader.onload = (evt) ->
$scope.$apply ($scope) ->
$scope.myImage = evt.target.result
reader.readAsDataURL(file)
angular.element(document.querySelector('#fileInput')).on('change', handleFileSelect)
And HTML:
<div class="form-group ng-scope">
<label class="col-sm-2 control-label" for="image">Image:</label>
<div class="col-sm-10">
<div class="cropFile">
<input id="fileInput" type="file">
</div>
</div>
</div>
I want to get '1' in console after I change some file in #fileInput
. But when I choose file I get nothing. How can I fix it? Thanks!
-
<input id="fileInput" type="file" ng-change="handleFileSelect()" />
– devqon Commented Jan 6, 2015 at 9:51 - I add it, add move handleFileSelect to $scope, but it doesn't work.. – malcoauri Commented Jan 6, 2015 at 9:59
- 1 any luck with this, having similar problem?? – Andrei Maieras Commented Aug 18, 2015 at 9:08
1 Answer
Reset to default 5Do this by making a directive.
I have created a directive name upload
HTML PART
<div><input style="margin: 0 auto;" type="file" id="fileInput" upload></div>
JS for Directive
myapp.directive('upload', function($rootScope) {
return {
restrict: 'EA',
link: function(scope, elem, attrs) {
elem.on("change" ,function(evt) {
var file = evt.currentTarget.files[0];
var reader = new FileReader();
reader.onload = function(evt) {
scope.$apply(function($scope) {
$rootScope.myImage = evt.target.result;
console.log($rootScope.myImage);
});
};
reader.readAsDataURL(file);
});
}
};
});
本文标签: javascriptAngularJs element on change eventStack Overflow
版权声明:本文标题:javascript - AngularJs element on change event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744887929a2630598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论