0

モバイルアプリケーション内でCordovaファイル転送アップロードを行うAngularサービスがあります。ファイルをメディアサーバーにアップロードしてオブジェクトを返します。私はmedia_responseプロパティにこのオブジェクトを渡そうとしていますが、私は運がありません - 私は間違っていますか?コーデックファイル転送内で角度サービス/ファクトリクラスのプロパティが更新されない

注意してください - 私はservicefactoryを使用してみましたし、まだ喜びを取得しないように思われている - $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 

答えて

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 -scopeを作成していない新しい機能の矢印表記を使用し、thisを使用したい/必要がある場合。

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

}); 
関連する問題