如何在Angular 6中进行同步调用

问题描述 投票:0回答:2

嗨我想尝试获取角度的用户详细信息,并根据我需要执行特定任务的用户值,我能够调用api但响应是花了一些时间,这就是为什么我无法检查条件。

ngOnInit() {

this.getUserDetails();

  if((this.user.PrivacyNotice)) 
  {
       ------
  }
  else
  {
       ------
  }
 }

getUserDetails() {
this.user.Email = "[email protected]";

this.bookingListSubscription = this.restService.getUserProfile(this.user).subscribe(
  data => {
    this.user = data;
  });
}

这里user是UserVM Model类型的对象,它包含PrivacyNotice布尔字段

以下是进行api调用的服务方法

getUserProfile(user: UserVM): Observable<any> {
    return this.http.post<UserVM>(this.getAPI('FetchUserProfile'),user,
  this.httpOptions).pipe(
    map(data => data),
    catchError(this.handleError<any>('getUserProfile'))
  );

}

如果我想检查从api返回的布尔标志PrivacyNotice的值,那么我怎么能得到这个。

angular angular6
2个回答
0
投票

只需在subscribe回调中完成任务。

this.bookingListSubscription = this.restService.getUserProfile(this.user).subscribe(
  data => {
    this.user = data;

    if((this.user.PrivacyNotice)) 
    {
         ------
    }
    else
    {
         ------
    }

  });

0
投票

你可以查看这个链接,了解如何进行异步调用qazxsw poi

© www.soinside.com 2019 - 2024. All rights reserved.