0

我有 Angular 服务,它在移动应用程序中执行 Cordova 文件传输上传 - 它将文件上传到媒体服务器并返回一个对象。我试图将此对象传递给该media_response属性,但我没有运气 - 我做错了什么?

请注意 - 我已经尝试使用serviceandfactory并且似乎仍然没有得到任何乐趣 -$cordovaFileTransfer upload由于某些奇怪的原因,块内的任何内容都没有传递给类属性。

我希望看到media_response与数据变量相同的输出,数据变量是从媒体服务器返回的对象,但它说no response

知道为什么我没有得到预期的响应吗?

// 预期响应

UploadService.media_response in the controller should be an object to match the console.log(data)

// 实际响应

UploadService.media_response is the string 'no response'

// 上传服务

abcdServices.service('UploadService', function($http, $localStorage, $location, $q, $rootScope, $cordovaFileTransfer) {

var UploadService = function() {
    this.upload_in_progress = false;
    this.progress = 0;
    this.media_response = '';
};

UploadService.prototype.cordovaFileUpload = function (filename, url, targetPath) {
    this.upload_in_progress = true;
    this.media_response = 'no response';

    var options = {
        id: new Date() . getTime()  + filename,
        fileKey: "file",
        fileName: filename,
        chunkedMode: false,
        mimeType: "multipart/form-data",
        params : {'fileName': filename}
    };

    $cordovaFileTransfer.upload(url, targetPath, options).then(
        function(result) {
            // Success!
            var data = JSON.parse(result.response);
            console.log('file uploaded - response:', data); // ALWAYS A OBJECT
            this.media_response = 'fake response2';
            UploadService.media_response = 'fake response3';

            if (angular.isDefined(data.id)) {
                angular.forEach(data, function(value, key) {
                    // image[key] = value;
                });
                // $scope.post.linkThumbnail = false;
                // $scope.post.video = '';
            } else if (angular.isDefined(data.message)) {
                // $scope.uploadError = data.message;

            } else {

            }
        }, function(err) {

        }, function (progress) {
            // constant progress updates
            this.progress = parseInt(100 * progress.loaded / progress.total) + '%';
        }
    );

};

return new UploadService();});

// 控制器

UploadService.cordovaFileUpload(filename, url, targetPath, options);
console.log(UploadService); // to view in js console
4

1 回答 1

0

您的this.变量的范围是最近的功能块,而不是服务/工厂。

var使用或在服务/工厂的根目录中创建变量let


abcdServices.service('UploadService', function($http, $localStorage, $location, $q, $rootScope, $cordovaFileTransfer) {

    let upload_in_progress = false;
    let progress = 0;
    let media_response = '';

    var UploadService = function() {
        upload_in_progress = false;
        progress = 0;
        media_response = '';
    };

    UploadService.prototype.cordovaFileUpload = function (filename, url, targetPath) {
        upload_in_progress = true;
        media_response = 'no response';

        // Rest of your code
    };

return new UploadService();});

如果您确实需要/想要使用this,请使用新的功能箭头符号,它不会this在功能块中创建新的 -scope。

abcdServices.service('UploadService', function(...) {

    this.upload_in_progress = 'test';
    this.progress = 0;
    this.media_response = '';

    someFunction = (param1, param2, ..) => {
        console.log(this.upload_in_progress) // 'test'
    }

});
于 2017-11-10T13:22:34.667 回答