Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 2基于值刷新http请求_Angular_Rxjs - Fatal编程技术网

Angular 2基于值刷新http请求

Angular 2基于值刷新http请求,angular,rxjs,Angular,Rxjs,由于我对angular还不熟悉,我想知道实现我想要的目标的最佳实践是什么 我想请求一个API,直到我得到我想要的结果,请求之间的间隔 我使用两个嵌套服务(GiftsService和ApiService): gives.service.ts: import { Injectable } from '@angular/core'; import { URLSearchParams } from '@angular/http'; import { environment } from '../../.

由于我对angular还不熟悉,我想知道实现我想要的目标的最佳实践是什么

我想请求一个API,直到我得到我想要的结果,请求之间的间隔

我使用两个嵌套服务(GiftsService和ApiService):

gives.service.ts:

import { Injectable } from '@angular/core';
import { URLSearchParams } from '@angular/http';
import { environment } from '../../../environments/environment';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

import { ApiService } from './api.service';
import { Gift } from '../models';

@Injectable()
export class GiftsService {
  constructor (
    private apiService: ApiService
  ) {}

  get(id): Observable<Gift> {
    return this.apiService.get('/gifts/' + id)
           .map(data => data);
  }
  ...
更新

我是怎么做到的

this.giftsService.get(this.gift.id)
      .switchMap(data => data.status !== 'success' ? Observable.throw(new Error('status not successful')) : Observable.of(data))
      .retryWhen(attempts => {
        return Observable
        .range(1,10)
        .zip(attempts, function(i) {
          return(i);
        })
        .flatMap(function(i) {
          return Observable.timer(1000);
        })
      })
      .subscribe((
        data => {
          this.gift = data
        })
      );

您可以这样做:

this.giftsService.get(this.gift.id)
.switchMap(data=>data.status!==“OK”?Observable.throw():Observable.of(data))
.retryWhen(错误=>{/*此处设置重试逻辑*/})
.subscribe(数据=>{})

您可以将
retryWhen
操作符与
范围
计时器
结合使用

get(path: string, params = new URLSearchParams()) {
   return this.http.get(path, { 
       headers: this.setHeaders(), search: params 
   })
   .map(response => {
     response = response.json();
     if(response.status !== 'ok') {
       throw new Error('status not okay');
     }
     return response;
   })
   .retryWhen(errors => {
     return Observable
       .range(1, 3 /* maximum retry attempts */)
       .zip(errors)
       .flatMap([attempt, error] => {
         if(attempt >= 3) {
           return Observable.throw(error);
         }
         return Observable.timer(attempt*500 /* the retry timeout which increases after each attempt */);
       })
   })
   .catch(this.formatErrors)
}

在Error函数中,重试命中服务,例如this.giftsService.get(this.gift.id).subscribe(数据=>{//if data.status!=“ok”重试,Error=>调用服务……=Error}),您可以使用rxjs retrywhen运算符,如下所示:
this.giftsService.get(this.gift.id)
      .switchMap(data => data.status !== 'success' ? Observable.throw(new Error('status not successful')) : Observable.of(data))
      .retryWhen(attempts => {
        return Observable
        .range(1,10)
        .zip(attempts, function(i) {
          return(i);
        })
        .flatMap(function(i) {
          return Observable.timer(1000);
        })
      })
      .subscribe((
        data => {
          this.gift = data
        })
      );
get(path: string, params = new URLSearchParams()) {
   return this.http.get(path, { 
       headers: this.setHeaders(), search: params 
   })
   .map(response => {
     response = response.json();
     if(response.status !== 'ok') {
       throw new Error('status not okay');
     }
     return response;
   })
   .retryWhen(errors => {
     return Observable
       .range(1, 3 /* maximum retry attempts */)
       .zip(errors)
       .flatMap([attempt, error] => {
         if(attempt >= 3) {
           return Observable.throw(error);
         }
         return Observable.timer(attempt*500 /* the retry timeout which increases after each attempt */);
       })
   })
   .catch(this.formatErrors)
}