angular实现changeFile上传[方法三]

View JSFiddle

view.html:

1
<input type="file" custom-on-change="uploadFile">

controller.js:

1
app.controller('myCtrl', function($scope){ $scope.uploadFile = function(event){ var files = event.target.files; }; });

directive.js:

1
2
3
4
5
6
7
8
9
app.directive('customOnChange', function() {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var onChangeHandler = scope.$eval(attrs.customOnChange);
element.bind('change', onChangeHandler);
}
};
});

View JSFiddle

参考出处