完成我创造这样的供应商:知道当HTTP请求从提供商Angular2
import {Injectable, Provider, Inject} from 'angular2/core';
import {Http, Response, Headers} from 'angular2/http';
import {Platform, Storage, SqlStorage,LocalStorage} from "ionic-angular";
@Injectable()
export class MyProvider {
constructor(@Inject(Platform)platform,@Inject(Http)http) {
this.http = http;
}
GetSomeOtherStuff() {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post(
'http://localhost:3000/getotherstuff',
{headers: headers}
).map(
(res:Response)=>res.json())
.subscribe(
(response) => {
//my response now can be used
},
(err) => {
},
() => {
}
); //end of subscribe
}
GetSomeStuff() {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post(
'http://localhost:3000/getstuff',
{headers: headers}
).map(
(res:Response)=>res.json())
.subscribe(
(response) => {
//my response now can be used
},
(err) => {
},
() => {
}
); //end of subscribe
}
然后我有一个页面。在这个页面的内部,当用户开始调用提供者getstuff()和getotherstuff()函数来获取一些数据时,数据加载时应该显示一个微调,但当请求完成时,页面应该知道这一点。
@Page({
templateUrl: 'build/pages/mypage/mypage.html',
providers: [MyProvider]
})
export class MyPage {
isLoadingSpinner = False;
constructor(_providerMessages: ProviderMessages) {
//when this page is come upon I would like to get the data from getstuff and getothersstuff.
// when it loads it should go into these two varaibles to be displayed on the page.
isLoadingSpinner = True; //page is laoding so show spinner
this.first_data = //getstuff from provider when loaded
this.second_data = //getotherstuff from provider when loaded
isLoadingSpinner = false; //page is done loading
}
}
}
基本上我想在网页中显示的数据时,它被加载,但是当我们仍在等待答复。能够捕捉到这种状态也让我能证明微调
see this [question](http://stackoverflow.com/questions/36554125/angular2-rxjs-updating-variable-after-g ETTING - 数据 - 从-HTTP-可观察到的/ 36554544) – kemsky