2017-04-23 5 views
0

私のIonicアプリで、特定のファイルをダウンロードしたい。 TransferObjectはすべてのページでアクセスできる必要があります。そのため、ダウンロード機能をグローバル変数に移動しました。グローバルに存在しない場合、ダウンロードはうまくいったことに注意してください。 は、ここで私が持っているものです。グローバルに移動したときに機能が動作しない

プロバイダ/グローバル/ global.ts

import {Injectable, ChangeDetectorRef} from '@angular/core'; 
import { Transfer, FileUploadOptions, TransferObject } from '@ionic-native/transfer'; 
import { File, Entry } from '@ionic-native/file'; 

@Injectable() 
export class Global { 

public progress_locatie1: string; 
public progress_locatie2: string; 

public progressbar_locatie1=false; 
public progressbar_locatie2=false; 

public downloadbutton_locatie1=true; 
public downloadbutton_locatie2=true; 

public playbutton_locatie1=false; 
public playbutton_locatie2=false; 

constructor(public ref: ChangeDetectorRef, public file: File, public transfer: Transfer) { 
    this.progress_locatie1 = '0'; 
    this.progress_locatie2 = '0'; 
    } 
public fileTransfer_locatie1: TransferObject = this.transfer.create(); 
public fileTransfer_locatie2: TransferObject = this.transfer.create(); 

setProgress(location, value) { 
    this['progress_'+location]=value; 
    this.ref.detectChanges(); 
} 

getProgress(location) { 
    return this['progress_'+location]; 
} 

setStatusOfObject(object,location, value) 
{ 
    this[object+'_'+location]=value; 
} 

getStatusOfObject(object,location) 
{ 
    return this[object+'_'+location]; 
} 

startDownload(locatie) 
{ 
    this.setStatusOfObject('downloadbutton',locatie,false); 
    this.setStatusOfObject('progressbar',locatie,true); 

    this['fileTransfer_' + locatie].download('https://someurl.com/'+locatie+'.mp4', this.file.dataDirectory + 'path/to/downloads/'+locatie+'.mp4').then((entry) => { 
     console.log('download complete: ' + entry.toURL()); 

     this.setStatusOfObject('progressbar',locatie,false); 
     this.setStatusOfObject('playbutton',locatie,true); 

    }, (error) => { 
     console.log('error'); 
     console.log(error); 
    }); 

    this['fileTransfer_' + locatie].onProgress(progressEvent => { 
     if (progressEvent.lengthComputable) { 
      console.log("### Download percentage ###: "+(progressEvent.loaded/progressEvent.total)); 
      this.setProgress(locatie,Math.round((progressEvent.loaded/progressEvent.total) * 100)); 
     } else { 
     } 
    }); 
} 

cancelDownload(locatie) 
{ 
    this.setStatusOfObject('downloadbutton',locatie,true); 
    this.setStatusOfObject('progressbar',locatie,false); 
    this.setStatusOfObject('playbutton',locatie,false); 
    this.setProgress(locatie,"0"); 
    this['fileTransfer_' + locatie].abort(); 
} 

} 

ページ/ somepage/somepage.ts

import {Global} from '../../providers/global/global'; 
export class SomePage { 
constructor(private global: Global) { 
} 

downloadnative(locatie) 
{ 
    this.global.startDownload(locatie); 
} 

cancel_download(locatie) 
{ 
    this.global.cancelDownload(locatie); 
} 
} 

ページ/ somepage/somepage.html

<button ion-button full color="light" *ngIf="global.getStatusOfObject('downloadbutton','locatie1')" (tap)="downloadnative('locatie1')">Download</button> 

ダウンロードボタンを押しても何も起こりません。コンソール出力なし、何もありません。 ダウンロード部がpages/somepage.tsになっても問題なく動作しましたが、私が言ったように、他のページのTransferObjectにもアクセスしたいと思います。

追加情報:startDownload関数自体が呼び出されますが、ダウンロードは開始されません。

+0

stubbedイオン性物質でhttp://stackoverflow.com/help/mcveを提供することを検討してください。あなたがIonicアプリケーションをデバッグできない場合、あなたはそれを自分で実行しています。あなたがコンソールに何も持っていないと信じるのは難しいです。まず、ChangeDetectorRefをサービスに注入することはできません。注入エラーが発生します。 – estus

+0

'ChangeDetectorRef'はエラーをスローしません。ダウンロード機能がsomepage.ts上にあったとき、setProgressはすでにglobal.ts内にあり、異なるページの進捗状況にアクセスします。これは 'detectChanges()'だけで動作します。コンソールには事がありますが、私はコンソールに何もエラーがないことを意味しました。たとえば、console.logをstartDownload関数に入れると、コンソールに正しく出力されます。 – binoculars

+0

ChangeDetectorRefはコンポーネント内にのみ存在します。エラーが発生しない場合は、Globalがモジュールプロバイダーではなくコンポーネントプロバイダーであることを意味します。定義および使用方法を指定する必要があります。 ChangeDetectorRefはコンポーネントごとに個別であるため、グローバルがシングルトンであると考えているので、これは恐らく間違っているでしょう。再度、MCVEが必要です。あなたは現在の状態の質問に対して良い答えを期待することはできません。これは完全な推測です。 – estus

答えて

0

多くの試行錯誤の後にわかりました。

public fileTransfer_locatie1: TransferObject = this.transfer.create(); 
public fileTransfer_locatie2: TransferObject = this.transfer.create(); 

:この変更

public fileTransfer_locatie1: TransferObject; 
public fileTransfer_locatie2: TransferObject; 
... 
startDownload(locatie) 
{ 
    this['fileTransfer_' + locatie] = this.transfer.create(); 
    ... 
} 

は私の問題を修正しました。

関連する問題