我的指示如下
angularApp.directive('fileModel', ['$parse','flash', function ($parse, flash) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function () {
console.log(scope.tools);
if (element[0].files[0].size <= 4000000) {
if (attrs.fileType != 'image' || element[0].files[0].type == "image/jpg" || element[0].files[0].type == "image/jpeg" || element[0].files[0].type == "image/png" || element[0].files[0].type == "image/gif" || element[0].files[0].type == "image/bmp") {
scope.$apply(function () {
modelSetter(scope, element[0].files[0]);
});
}
else {
flash.pop({
title: 'Warning',
body: 'Please select logo of type jpg,png,gif or bmp',
type: 'warning'
});
}
}
else {
flash.pop({
title: 'Warning',
body: 'Please select max 4 MB in size',
type: 'warning'
});
}
});
}
};
}]);
其工作的个人,但如果我在ng-repeat中使用它,如下所示
<div class="form-group browse-file" ng-repeat="casestudy in tools.casestudies">
<input type="file" file-model="tools.casestudies[$index]", file-type="doc" class="col-lg-4 browse-file-info" />
</div>