AngularJs:如何检查文件输入字段中的更改?我对角不熟悉。我试图从HTML‘file’字段读取上传的文件路径,每当该字段发生“更改”时。如果我使用“onChange”,它会起作用,但当我用角度方式使用“ng-Change”时,它就不起作用了。<script>
var DemoModule = angular.module("Demo",[]);
DemoModule .controller("form-cntlr",function($scope){
$scope.selectFile = function()
{
$("#file").click();
}
$scope.fileNameChaged = function()
{
alert("select file");
}});</script><div ng-controller="form-cntlr">
<form>
<button ng-click="selectFile()">Upload Your File</button>
<input type="file" style="display:none"
id="file" name='file' ng-Change="fileNameChaged()"/>
</form> </div>fileNameChage()从来不打电话。Firebug也不会显示任何错误。
3 回答
扬帆大鱼
TA贡献1799条经验 获得超9个赞
view.html:
<input type="file" custom-on-change="uploadFile">
js:
app.controller('myCtrl', function($scope){ $scope.uploadFile = function(event){ var files = event.target.files; };});
js:
app.directive('customOnChange', function() { return { restrict: 'A', link: function (scope, element, attrs) { var onChangeHandler = scope.$eval(attrs.customOnChange); element.on('change', onChangeHandler); element.on('$destroy', function() { element.off(); }); } };});
森林海
TA贡献2011条经验 获得超2个赞
<input data-file id="id_image" name="image" ng-model="my_image_model" type="file">
app.directive('file', function() { return { require:"ngModel", restrict: 'A', link: function($scope, el, attrs, ngModel){ el.bind('change', function(event){ var files = event.target.files; var file = files[0]; ngModel.$setViewValue(file); $scope.$apply(); }); } };});
- 3 回答
- 0 关注
- 529 浏览
添加回答
举报
0/150
提交
取消