javascript - Dropzone.js 回调访问 Angular 作用域

标签 javascript angularjs dropzone.js

我构建了一个简单的 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/

相关文章:

javascript - 在回调函数中进行新的异步调用

iphone - 从手机上传到Angular的图像是横向的或上下颠倒的

html - 将所有子项的高度设置为父项固定高度的 50%

javascript - 扩展 Controller 组件 - angularjs

css - 如何使用 DropZone.js 获得质量更好的缩略图?

rest - 使用 Dropzone.js 上传 BLOB/ArrayBuffer

javascript - dropzone 只上传一个文件

javascript - 如何在 JSON.stringify 之后删除\and?

javascript - 在html Canvas 中围绕固定中心旋转圆弧

javascript - 如何在不重新加载页面或更改 html 的情况下自动填写网页上的表单