移动到全局时功能不起作用

时间:2017-04-23 18:20:54

标签: angular ionic-framework

在我的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函数本身被调用,但下载没有开始。

1 个答案:

答案 0 :(得分: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();
  ...
}

解决了我的问题。