我有指令“notes”,它基本上是一个便笺小部件,我可以在任何页面上添加它,只需为其添加名称和 id,以允许人们向该项目添加便笺。
用户现在也应该能够使用ng-file-upload将文件上传到他们的笔记,但我很难让$scope.files
ng-file-upload 指令填充。我很确定它像往常一样与范围一样愚蠢,但我无法弄清楚。
那么知道为什么在我选择文件时没有填充 $scope.files 吗?
指示:
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>