angular - 如何等到函数以角度完全执行
问题描述
嗨,我正在尝试以角度获取用户详细信息,并根据我需要执行特定任务的用户值,我能够调用 api 但响应需要一些时间,这就是我无法检查条件的原因。
ngOnInit() {
this.getUserDetails();
if(this.user.PrivacyNotice) //getting undefined here
{
}
else
{
}
}
getUserDetails() {
this.user.Email = "test@test.com";
this.bookingListSubscription =
this.restService.getUserProfile(this.user).subscribe(
data => {
this.user = data;
});
}
这里 user 是包含 PrivacyNotice 布尔字段的 UserVM 模型类型的对象
以下是进行 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 的值,那么我该如何实现这一点。
解决方案
通常,只要您处于加载状态,就会显示加载指示器:
例子:
loading = ture;
ngOnInit() {
this.getUserDetails();
}
getUserDetails() {
this.user.Email = "test@test.com";
this.bookingListSubscription =
this.restService.getUserProfile(this.user).subscribe(
data => {
this.user = data;
if((this.user.PrivacyNotice))
{
------
}
else
{
------
}
loading = false;
});
}
并在您的 *.html 中
<div *ngIf="loading else #loaded">
Loading...
</div>
<ng-template #loaded>
The data is loaded, display them as you wish
</ng-template>
推荐阅读
- json - 无法在 Java 中反序列化 gson 数组的对象
- python - 从python中的另一个模块导入一个模块
- c# - 在程序中加载依赖项(不同版本的dll)的常见做法
- django - 我想要数据寿命,如果我输入“a”所有与 a 相关的数据,我想要所有字母
- reactjs - 是否有任何 React 组件可以像手风琴一样显示数据
- operating-system - 工艺段中的空白部分是什么。[操作系统]
- java - 如何在多模块 android 项目(kotlin + java)上配置 SonarQube(使用 jacoco)?
- angular - spyOn 即使通过函数也不会被调用
- kubernetes - Kubernetes DNS 在反应应用程序中不起作用
- yii2 - Yii 2 模块明智的配置设置