javascript - Angular 指令 ngIf 未按预期工作
问题描述
我们正在尝试将数据从一个组件传递到另一个组件,下面是我们正在采用的方法。当没有数据时,我们要显示错误消息
<div *ngIf="showGlobalError">
<h6>The reporting project doesn't have any Shippable Items</h6>
</div>
和 component.ts 就像
showGlobalError = true;
constructor(private psService: ProjectShipmentService, private pdComp: ProjectDetailsComponent) {
this.psService.tDate.subscribe(x => this.cachedResults = x);
}
ngOnInit() { }
ngDoCheck() {
if (this.cachedResults.length > 0 && this.count <= 1) {
this.showGlobalError = false;
this.populateArrays();
this.count++;
}
}
populateArrays() {
this.reportingProject = [this.pdComp.rProjectNumber];
this.projectSalesOrder = this.pdComp.rSalesOrder;
this.clearFilter();
........
问题是即使 this.cachedResults 中有数据 this.cachedResults.length 在几秒钟内不等于 '0' '报告项目没有任何可发货项目' 显示在页面中,然后显示数据我不确定这是否与 ngDoCheck() 相关。任何帮助是极大的赞赏
解决方案
由于默认值为showGlobalError
true,因此页面加载会显示错误消息。
请将其设为默认 false 并在this.cachedResults.length
is0
或this.cachedResults
isundefined
或this.cachedResults
is时设为 true null
。
希望这能解决您的问题。
推荐阅读
- java - 是否可以通过改造在 listView 中获取 Double 值数组?
- python - 使用范围和列表方法构造列表
- flutter - Flutter 中的 setState() 方法直到 for 循环中的最后一次调用才更新视图
- django - 我想向 django html 中的按钮添加一个函数,这样当我单击该按钮时,它会将 +1 添加到现有数据
- c++ - C++中条件运算符的使用
- or-tools - VRP:可以检测具有空域的变量吗?
- javascript - JS浏览器游戏——避免初始化所有对象
- javascript - 如何在前端使角色有条件?
- javascript - 使用 jQuery 修改 DOM 元素后如何重新加载函数(tempus dominus)
- javascript - 在css中做一个下拉按钮