gpt4 book ai didi

javascript - AngularJS ng-file-upload 在 ng-repeat 中不起作用

转载 作者:行者123 更新时间:2023-11-30 12:20:20 26 4
gpt4 key购买 nike

我有指令“notes”,它基本上是一个笔记小部件,我可以在任何页面上添加它,只需向它添加名称和 id 即可允许人们向该项目添加笔记。

用户应该能够使用 ng-file-upload 将文件上传到他们的笔记中现在也是,但我很难让 $scope.files 由 ng-file-upload 指令填充。我很确定示波器和往常一样有些愚蠢,但我无法弄清楚。

那么知道为什么当我选择一个文件时 $scope.files 没有被填充吗?

Plunker Link

指令:

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);
});
}

}]
};
}]);

模板:

<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>

最佳答案

我来晚了,但是您在更新 $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>

有关作用域如何在 Angular 检查中工作的更多信息,请查看 https://github.com/angular/angular.js/wiki/Understanding-Scopes

关于javascript - AngularJS ng-file-upload 在 ng-repeat 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31313845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com