[英]Angular ngif does not change with variable
I've got a service that loads a list of people from a local database, and that all works great right now.我有一个从本地数据库加载人员列表的服务,现在一切都很好。 I get a list displayed properly... I even got a filter working with the list and it works great... but when I attempt to add a loading wheel to the filter, it will not display...
我得到了一个正确显示的列表......我什至有一个过滤器可以处理该列表并且效果很好......但是当我尝试向过滤器添加加载轮时,它不会显示......
Right at the beginning of my AppComponent, I initialize a variable called "loadingData" to false.在 AppComponent 的开头,我将一个名为“loadingData”的变量初始化为 false。
export class AppComponent implements OnInit, AfterViewInit {
loadingData: boolean = false;
Then in my filter method, I attempt to set the variable to true, and then back to false.然后在我的过滤器方法中,我尝试将变量设置为 true,然后再设置为 false。
public doFilter = (value: string) => {
this.loadingData = true;
setTimeout(() =>
{
this.dataSource.filter = value.trim().toLocaleLowerCase();
},
5000);
this.loadingData = false;
}
The filter method works, but the HTML side of things never changes... filter 方法有效,但事物的 HTML 方面永远不会改变......
I've got a simple message I want to display when "loadingData" is true:当“loadingData”为真时,我想显示一条简单的消息:
<ng-container *ngIf="loadingData">
<p>Loading data, please wait...</p>
</ng-container>
But the ngIf function doesn't appear to want to work in real-time... if I initiallize "loadingData" to true instead of false then the message shows up, but it never disappears...但是 ngIf 函数似乎不想实时工作......如果我将“loadingData”初始化为 true 而不是 false 那么消息会显示,但它永远不会消失......
How can I get this message to show up and disappear as I change the variable?如何在更改变量时显示和消失此消息?
you should set loadingData to false only after async operation finishes:只有在异步操作完成后,您才应该将 loadingData 设置为 false:
public doFilter = (value: string) => {
this.loadingData = true;
setTimeout(() => {
this.dataSource.filter = value.trim().toLocaleLowerCase();
this.loadingData = false;
}, 5000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.