gpt4 book ai didi

angular - *ngIf 在 angular2 中的组件类变量上

转载 作者:行者123 更新时间:2023-12-01 23:17:27 25 4
gpt4 key购买 nike

我想在标志变量为 true 时显示加载程序,并在标志变量为 false 时隐藏它(双向数据绑定(bind)),但我不知道如何将 *ngIf 与组件变量一起使用

app.component.ts

import { Component, OnInit } from '@angular/core';
import { User } from '../../models/users.model';
import { UserServices } from '../../services/User.service';
@Component({
selector: 'app-default',
templateUrl: './default.component.html',
styleUrls: ['./default.component.css']
})
export class defaultComponent implements OnInit {
public flag: boolean;
userList: User[];
constructor(private _service: UserServices) {
this.flag = true;
}
ngOnInit() {
this.getData();
}
getData() {
this.flag = true;
this._service.loadData().subscribe( response => { this.userList = response; });
this.flag = false;
}
}

default.component.html

    <div *ngIf="flag == true" class="loader">Loading...</div>
<div class="content">
<!--my html-->
</div>

我想仅在调用服务时显示加载器 div,并在调用完成后隐藏它。

最佳答案

响应返回时将标记设置为 false。否则,您将立即将其设置为 false:

getData() {
this.flag = true;
this._service.loadData().subscribe( response => {
this.userList = response;
this.flag = false;
});
}

此外,您不需要显式检查 true:

*ngIf="flag"

如果您愿意,您可以在声明时初始化您的标志,而不是在构造函数中进行:

public flag: boolean = true;

关于angular - *ngIf 在 angular2 中的组件类变量上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48028737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com