Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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 角度:发出post请求后,如何触发get请求并使用修改后的值更新组件?_Angular_Http_Rxjs - Fatal编程技术网

Angular 角度:发出post请求后,如何触发get请求并使用修改后的值更新组件?

Angular 角度:发出post请求后,如何触发get请求并使用修改后的值更新组件?,angular,http,rxjs,Angular,Http,Rxjs,getAllSchools()从后端返回学校列表。当应用加载时,我发出get请求并将学校列表加载到一个名为schools的数组中 addSchool()添加新学校 每次addSchool()完成时,我都要刷新schools数组以包含新学校。我该怎么做 学校服务。ts getAllSchools(): Observable<ISchool[]> { return this.http.get<ISchool[]>(this.url); } addSch

getAllSchools()
从后端返回学校列表。当应用加载时,我发出get请求并将学校列表加载到一个名为schools的数组中

addSchool()
添加新学校

每次
addSchool()
完成时,我都要刷新schools数组以包含新学校。我该怎么做

学校服务。ts

  getAllSchools(): Observable<ISchool[]> {
    return this.http.get<ISchool[]>(this.url);
  }


  addSchool(school: ISchool): Observable<ISchool> {
    this.schoolsSubject$.next();
    return this.http.post<ISchool>(this.url, school, {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    });
  }
  ngOnInit(): void {
    this.getSchools();
  }


  getSchools(): void {
    this.schoolService.getAllSchools().subscribe({
      next: schools => {
        this.schools = schools;
        this.filteredSchools = schools;
      },
      error: err => this.errorMessage = err
    });
  }
  onSubmit(form: NgForm) {
    this.schoolService.addSchool(this.model)
      .subscribe(
        (data: ISchool) => console.log(data),
        (err: any) => console.log(err)
      ); 
  }
添加scshool组件。ts

  getAllSchools(): Observable<ISchool[]> {
    return this.http.get<ISchool[]>(this.url);
  }


  addSchool(school: ISchool): Observable<ISchool> {
    this.schoolsSubject$.next();
    return this.http.post<ISchool>(this.url, school, {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    });
  }
  ngOnInit(): void {
    this.getSchools();
  }


  getSchools(): void {
    this.schoolService.getAllSchools().subscribe({
      next: schools => {
        this.schools = schools;
        this.filteredSchools = schools;
      },
      error: err => this.errorMessage = err
    });
  }
  onSubmit(form: NgForm) {
    this.schoolService.addSchool(this.model)
      .subscribe(
        (data: ISchool) => console.log(data),
        (err: any) => console.log(err)
      ); 
  }

在add-school-component.ts中

  @Output() newSchool: EventEmitter<any> = new EventEmitter();

  onSubmit(form: NgForm) {
    this.newSchool.emit(this.model);
  }

您可以使用
BehaviorSubject
保存数据,然后点击
再次调用

export class YourService {
  private _schools: BehaviorSubject<ISchool[]>;

  constructor(
    private http: HttpClient,
  ) {
    this._schools = new BehaviorSubject([]);
  }

  getAllSchools(): Observable<ISchool[]> {
    this._getAllSchools();

    return this._schools.asObservable();
  }

  addSchool(school: ISchool): Observable<ISchool> {
    return this.http.post<ISchool>(this.url, school, {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    }).pipe(
      // after successfully adding a school you call once again get all schools
      tap(() => this._getAllSchools()),
    );
  }

  private _getAllSchools(): void {
    this.http.get<ISchool[]>(this.url).subscribe(
      schools => this._schools.next(schools),
    );
  }
}
导出类服务{
私立学校:行为主体;
建造师(
私有http:HttpClient,
) {
此._schools=新行为主体([]);
}
getAllSchools():可观察{
这个;
将此返回。_schools.asObservable();
}
addSchool(学校:ISchool):可见{
返回this.http.post(this.url,school{
标题:新的HttpHeaders({
“内容类型”:“应用程序/json”
})
}).烟斗(
//成功添加一所学校后,您再次呼叫“获取所有学校”
点击(()=>this.\u getAllSchools()),
);
}
私立_getAllSchools():无效{
this.http.get(this.url).subscribe(
学校=>这个学校。下一个(学校),
);
}
}

schools数组是应用程序组件,addschool在schools服务中。所以,不能这样做..add-school-component.ts是app-component.ts的子级吗?