ngf-max-size $ digest()错误与angular.js中的ng-file-upload [英] ngf-max-size $digest() error with ng-file-upload in angular.js
问题描述
这是源代码.
<div class="thumbnail" ngf-accept="'image/*'" ngf-pattern="'*.jpg,*.jpeg,*.gif,*.png'" ngf-max-size="1MB">
矫正性HTML模板"下方.
Below the Derective Html Template.
angular.module('ptgui.imagebox',[
'ngFileUpload'
])
.directive('ptguiImagebox', ['$compile', function($compile) {
return {
templateUrl: 'scripts/libs/ptgui/ptgui-imagebox.html',
restrict: 'E',
require: 'ngModel',
scope: {
ngModel: "="
},
replace: true,
link: function ($scope, element, attrs) {
$scope.$watch('ngModel',function(newFile, oldFile){
if (newFile) {
$scope['ngModel'] = newFile;
} else {
$scope['ngModel'] = oldFile;
}
});
}
};
}]);
指令下方.
当我上传图片超过max_size时,下面始终出现$ digest()错误.
When I upload image over max_size, $digest() error comes up consistently below.
我还有一个问题. 找到解决此问题的解决方案后,当有人上传图片超过max_size时,我会发出警报消息.
and I have one more question. after I find solution that resolve this issue, I will make alert message when someone upload image over max_size.
谢谢您的回答.
推荐答案
您不应在watch内部更改模型,因为它会创建循环.如果您只需要在文件上传时提醒大小是否超过上限,就可以查看状态.
You shouldn't change your model inside watch since it creates a loop. If you just need to alert when a file is uploaded whether the size is above you can just check the status.
假设您输入以下内容:
<input type="file" ngf-select ng-model="picFile" name="file"
accept="image/*" ngf-max-size="1MB" required
ngf-model-invalid="errorFile">
然后,您只需要在html中显示它,即可显示超出限制大小的警报:
Then you just need to have this in your html to show the limit size exceeded alert:
<i ng-show="myForm.file.$error.maxSize">File too large
{{errorFile.size / 1000000|number:1}}MB: max 1M</i>
而且您也不需要指令来处理它.
And also you don't need a directive to handle that.
在此处中查看有关验证的更多信息.
Check here for more info around validation.
这篇关于ngf-max-size $ digest()错误与angular.js中的ng-file-upload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!