离子2计划服务



我有一个离子2项目,客户通过自定义提供商称为服务的服务来检查其余单元。目前,客户可以输入参考号,提供商将像往常一样返回单位,一切正常。我想要的是在后台定期调用此服务,说一次每30分钟检查单元,以便当单元低于某些值时,我可以通知客户。

getunitsProvider:

    import { Injectable } from '@angular/core';
    import { Http } from '@angular/http';
    import 'rxjs/add/operator/map';
    import { Observable } from 'rxjs/Rx';
    import { Meter } from '../models/meters';
    @Injectable()
    export class GetUnitsProvider {
      constructor(public http: Http) {} 
        // get the customer units
        loadUnitsBalance(id:number): Observable<Meter[]> {
        return this.http.get(`http://example.com/balance.pl?referencenumber=${id}&api=json`)
          .map(res => <Meter[]>res.json());
        }
    }

unitspage.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Meter } from '../../models/meters';
import { GetUnitsProvider } from '../../providers/get-units-provider';
@Component({
  selector: 'units-page',
  templateUrl: 'unitspage.html'
})
export class UnitsPage{
  reading: Meter[];
  clientReferenceNumber:any;
  constructor(public navCtrl: NavController,public params:NavParams,private getUnitsProvider: GetUnitsProvider) {
    this.clientReferenceNumber = params.get("clientMeterNumber");
    getUnitsProvider.loadUnitsBalance(this.clientReferenceNumber).subscribe(reading => {
     console.log(reading);
     this.reading=reading; 
    });
  }
}

在您的服务中:

constructor(public http: Http) {
  Observable.interval(30 * 60 * 1000)
            .switchMap(this.http.get(`...`))
            .map(res => res.json())
            .subscribe(res => this.check(res))
}
private check(res: <Meter[]>) {
  // check whether to notify the user
}

然后在模块中使用它(您无需在任何地方注入服务):

providers: [
    {
        provide: APP_INITIALIZER,
        use: GetUnitsProvider,
        deps: [Http],
        multi: true
    }
],

相关内容

  • 没有找到相关文章

最新更新