尝试加载先前从服务器上传的文件时,会产生以下错误。
// Load existing files:
$.ajax({
url: $('#fileupload').fileupload('option', 'url'),
dataType: 'json',
context: $('#fileupload')[0]
}).done(function (result) {
$(this).fileupload('option', 'done').call(this, $.Event('done'), {result: files});
});
//Exception
jquery.fileupload-angular.js:130 Uncaught TypeError: Cannot read property '$apply' of undefined
at done (jquery.fileupload-angular.js:130)
at <anonymous>:2:47
FileUploadController
中的以下代码段将$scope
放入文件数据中。
$element.fileupload(angular.extend(
{scope: $scope},
fileUpload.defaults
)).on('fileuploadadd', function (e, data) {
data.scope = $scope;
答案 0 :(得分:1)
所以“hacky”的解决方案是将一个服务注入指令的控制器并调用generateFileObject(files)
方法。
.directive('ngUploadForm', [function () {
return {
restrict: 'E',
templateUrl: 'templates/uploadform.html',
scope: {
allowed: '@',
url: '@',
autoUpload: '@',
sizeLimit: '@',
ngModel: '=',
name: '@'
},
controller: ['$scope', '$element', 'fileUpload','FileService', function ($scope, $element, fileUpload, FileService)
[other code]
// pre-populate file list
FileService.getSavedFiles()
.done(function(files){
generateFileObject(files)
});
FileService代码(基本jquery ajax)
this.getSavedFiles = function(){
return $.ajax({
url: this.getFileUrl(),
dataType: 'json'
});