在Angular7中,如何从api的.subscribe http响应中获取对象响应

时间:2019-02-08 10:48:55

标签: spring-boot angular7

我希望在添加新对象“ puits”时更新列表。 所以我需要将我的对象“ pts”作为我刚刚添加的“ puits”,作为订阅的响应,或者也许是另一种方法? 我想我可以使用httpOptions之类的东西来获得对象的目标, 并使用标题的“位置”使用功能get?

//my code in my component :  
puits: Puits[] = [];

  pts: Puits = {
    nom: 'OP100',
    idSite: 1,
    idtypepuits: 1,
  };

ngOnInit() {
    this.getPuits();
  }

add(nom: string): void {
    nom = nom.trim();
    if (!nom) { return; }
    this.puitsService.addPuits({ nom } as Puits)
     .subscribe( pts  => {this.puits.push(pts);
      });
  }    


//my code in my service :     
addPuits (puits: Puits): Observable<Puits> {
  return this.http.post<Puits>(this.puitsUrl, puits, httpOptions).pipe(
    tap((newPuits: Puits) => this.log(`Puits ajouté au nom=${newPuits.nom}`)),
    catchError(this.handleError<Puits>('addPuits'))
  );
}

//my server response when i add a puits :    
Location →http://localhost:9090/Base_Puits/Puits/272    
Content-Length →0    
Date →Thu, 07 Feb 2019 09:54:58 GMT

0 个答案:

没有答案