我构建了一个简单的 AngularJS 指令来在元素上实现 Dropzone.js。 我想在指令之外使用 ng-repeat 显示上传的文件,但我无法让它工作,因为元素的“addedfile”回调似乎正在创建数组的副本(scope.files)。回调可以读取数组(或数组的副本)但是当我在其上推送一个新元素时不会影响 ng-repeat。我怎样才能让它发挥作用?
.directive('dropzone', function() {
return {
restrict: 'EA',
link: function(scope, el, attrs) {
el.dropzone({
url: attrs.url,
maxFilesize: attrs.maxsize,
init: function() {
scope.files.push({file: 'added'}); // here works
this.on('success', function(file, json) {
});
this.on('addedfile', function(file) {
scope.files.push({file: 'added'}); // here doesn't work
});
}
})
}
}
});
最佳答案
由于这种情况发生在 Angular 领域之外,您必须通过将更改包装在 $apple 中来通知 Angular 更改:
this.on('addedfile', function(file) {
scope.$apply(function(){
scope.files.push({file: 'added'});
});
});
关于javascript - Dropzone.js 回调访问 Angular 作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16856137/