angular-file-upload更新范围onCompleteItem

时间:2015-03-13 12:32:54

标签: javascript angularjs angular-file-upload

所以我在我的范围内有一个名为$scope.selectedComponent

的值

然后我有以下uploader

    var uploader = $scope.uploader = new FileUploader({
    url: 'user_resources/upload.php',
    formData: [{module_id: $state.params.id, component: $scope.selectedComponent}]
});

// FILTERS

uploader.filters.push({
    name: 'customFilter',
    fn: function(item /*{File|FileLikeObject}*/, options) {
        return this.queue.length < 10;
    }
});

// CALLBACKS

uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/, filter, options) {
    console.info('onWhenAddingFileFailed', item, filter, options);
};
uploader.onAfterAddingFile = function(fileItem) {
    console.info('onAfterAddingFile', fileItem);
};
uploader.onAfterAddingAll = function(addedFileItems) {
    console.info('onAfterAddingAll', addedFileItems);
};
uploader.onBeforeUploadItem = function(item) {
    console.info('onBeforeUploadItem', item);
};
uploader.onProgressItem = function(fileItem, progress) {
    console.info('onProgressItem', fileItem, progress);
};
uploader.onProgressAll = function(progress) {
    console.info('onProgressAll', progress);
};
uploader.onSuccessItem = function(fileItem, response, status, headers) {
    console.info('onSuccessItem', fileItem, response, status, headers);
};
uploader.onErrorItem = function(fileItem, response, status, headers) {
    console.info('onErrorItem', fileItem, response, status, headers);
};
uploader.onCancelItem = function(fileItem, response, status, headers) {
    console.info('onCancelItem', fileItem, response, status, headers);
};
uploader.onCompleteItem = function(fileItem, response, status, headers) {
    console.info('onCompleteItem', fileItem, response, status, headers);
};
uploader.onCompleteAll = function() {
    console.info('onCompleteAll');
};

callback函数:onCompleteItem是我想更新selectedComponent对象的地方但是在该函数范围内我无法获得scope <的变量/ p>

我的问题是如何在文件上传后更新我的变量?

2 个答案:

答案 0 :(得分:2)

确保$ scope已加载到您的控制器中:

.controller('AppController', ['$scope', 'FileUploader', function($scope, FileUploader) {

之后,您可以从此控制器的任何位置更新$ scope.selectedComponent的值。

$scope.selectedComponent = "not uploaded";

    uploader.onCompleteItem = function (fileItem, response, status, headers) {
                  $scope.selectedComponent = "upload complete";
};

答案 1 :(得分:1)

你可以通过这种方式获得你的范围

var scope = angular.element(document.getElementById('MyCtrl')).scope();

没有测试,但它应该可以工作。