简体   繁体   English

AngularJS ng-file-upload在ng-repeat中无法正常工作

[英]AngularJS ng-file-upload not working inside ng-repeat

I have directive "notes" which is basically a notes widget that I can add on any page and just add a name and id to it to allow people adding notes to that item. 我有指令“笔记”,它基本上是一个笔记小部件,我可以在任何页面上添加,只需添加一个名称和ID,以允许人们添加该项目的注释。

The users should be able to upload files to their notes using ng-file-upload as well now but I struggle to get the $scope.files populated by the ng-file-upload directive. 用户应该能够使用ng-file-upload将文件上传到他们的笔记中,但是我很难获得由ng-file-upload指令填充的$scope.files I'm pretty sure it is as usual something stupid with the scope but I can't figure it out. 我很确定它像往常一样愚蠢的范围,但我无法弄明白。

So any idea why $scope.files gets not populated when I select a file? 所以,当我选择一个文件时,为什么$ scope.files没有填充?

Plunker Link Plunker Link

Directive: 指示:

angular.module('app').directive('notes', [function () {
    return {
        restrict: 'E',
        templateUrl: 'notes.html',
        scope: {
            itemId: '@',
            itemModel: '@'
        },
        controller: ['$scope', 'Upload', function($scope, Upload) {
            $scope.files = [];
            $scope.notes = [
              {title: 'first title'},
              {title: 'second title'}
            ];

            $scope.$watch('files', function (files) {
                console.log(files);
                $scope.formUpload = false;
                if (files != null) {
                    for (var i = 0; i < files.length; i++) {
                        $scope.errorMsg = null;
                        (function (file) {
                            uploadUsingUpload(file);
                        })(files[i]);
                    }
                }
            });

            function uploadUsingUpload(file) {
                file.upload = Upload.upload({
                    url: '/api/v1/notes/upload_attachment',
                    method: 'POST',
                    //headers: {
                    //  'my-header': 'my-header-value'
                    //},
                    //fields: {username: $scope.username},
                    file: file,
                    fileFormDataName: 'file'
                });

                file.upload.then(function (response) {
                    $timeout(function () {
                        file.result = response.data;
                    });
                }, function (response) {
                    if (response.status > 0)
                        $scope.errorMsg = response.status + ': ' + response.data;
                });

                file.upload.progress(function (evt) {
                    // Math.min is to fix IE which reports 200% sometimes
                    file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
                });

                file.upload.xhr(function (xhr) {
                    // xhr.upload.addEventListener('abort', function(){console.log('abort complete')}, false);
                });
            }

        }]
    };
}]);

Template: 模板:

<ul>
  <li ng-repeat="note in notes">
    {{note.title}}
    <div ngf-select ng-model="files"><b>upload</b></div>
  </li>
</ul>
<pre style="border: 1px solid red;">{{files | json}}</pre>

I'm kind of late here but the reason why you're having a problem updating the $scope.files property is because a new scope is being created within your ng-repeat. 我有点迟到了,但是你在更新$ scope.files属性时遇到问题的原因是因为你的ng-repeat中正在创建一个新的范围。

<ul>
    <li ng-repeat="note in notes">
        {{note.title}}
        <div ngf-select ng-model="$parent.files"><b>upload</b></div>
    </li>
</ul>
<pre style="border: 1px solid red;">{{files | json}}</pre>

For more information about how scoping works in angular check out https://github.com/angular/angular.js/wiki/Understanding-Scopes 有关角度检查中作用域如何工作的更多信息,请访问https://github.com/angular/angular.js/wiki/Understanding-Scopes

ng-file-upload sets the selected files object in the "files" model (or the attribute tied to ng-model) in the enclosing scope. ng-file-upload在封闭范围内的“files”模型(或绑定到ng-model的属性)中设置所选文件对象。 In this case the enclosing scope is ng-repeat. 在这种情况下,封闭范围是ng-repeat。 If you set an attribute "files" in note object and set it as ng-model it should work. 如果在note对象中设置属性“files”并将其设置为ng-model,它应该可以工作。

http://plnkr.co/edit/oVpgrSWQAcFdV26aVKv7?p=preview http://plnkr.co/edit/oVpgrSWQAcFdV26aVKv7?p=preview

<ul>
  <li ng-repeat="note in notes" >
    {{note.title}}
    <div ngf-select ng-model="note.files"><b>upload</b></div>
  </li>
</ul>

  // In directive
  $scope.notes = [{
    title: 'first title',
    files: []
  }, {
    title: 'second title',
    files: []
  }];

 //And you can watch the file model
  $scope.$watch(function($scope) {
    return $scope.notes.
    map(function(note) {
      return note.files;
    });
  }, function(files) {
      console.log('Files' + files);
  }, true)

I added the dynamic id with ngf-select and it's fine working for me. 我用ngf-select添加了动态id,它对我很好。

<ul>
 <li ng-repeat="note in notes">
   {{note.title}}
   <div ngf-select id="file{{$index}}" ng-model="files"><b>upload</b></div>
  </li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM