Angular 2.安排一项服务
我有一个ionic 2项目,客户通过定制提供商调用的服务检查剩余的单元。此时,客户可以输入参考号,供应商将一如既往地返回设备,一切正常。我想要的是在后台定期呼叫此服务,比如说每30分钟检查一次,以便当单位低于某个值时,我可以通知客户 GetUnitsProvider:Angular 2.安排一项服务,angular,ionic-framework,ionic2,Angular,Ionic Framework,Ionic2,我有一个ionic 2项目,客户通过定制提供商调用的服务检查剩余的单元。此时,客户可以输入参考号,供应商将一如既往地返回设备,一切正常。我想要的是在后台定期呼叫此服务,比如说每30分钟检查一次,以便当单位低于某个值时,我可以通知客户 GetUnitsProvider: import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/oper
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());
}
}
为您服务:
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
}
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
}
],