【发布时间】:2017-12-20 17:11:16
【问题描述】:
我有这两个基于数据显示/隐藏的 div;在这种情况下,如果某家公司有账单卡,它将显示卡的信息,否则,它将显示您可以添加该信息的 div。但只有一个 div 显示,我不知道为什么。代码:
TS
public comapanyHasBilling(companyId: string) {
const searchID = this.billingList.find(item => item.companyId === item.companyId)
if (searchID !== undefined){
console.log(searchID.companyId)
}
});
}
HTML(第一个 div)
<!--If there is a card-->
<div *ngIf="!comapanyHasBilling(entity.id)" class="icon-company-title">
<div class="business-icon">
<i class="material-icons md-18">credit_card</i>
</div>
<span class="company-card-title">Credit Card</span>
<button class="" mat-icon-button [matMenuTriggerFor]="menu">
<i class="material-icons edit">mode_edit</i>
</button>
</div>
<!-- Content credit card -->
<mdl-card-title mdl-card-expand fxLayout="column" fxLayoutAlign="start start" fxLayoutGap="3px">
<span class="company-card-content company-card-content-edges">cardRef</span>
<span class="company-card-content">card</span>
<span class="company-card-content">test</span>
<span class="company-card-content company-card-content-edges" fxLayoutGap="5px">Name</span>
</mdl-card-title>
还有另一个 div
<!--If there is no card-->
<mdl-card-title *ngIf="!comapanyHasBilling(entity.id)" mdl-card-expand fxLayout="column" fxLayoutAlign="center center" fxLayoutGap="30px">
<span class="company-card-title">Payment Method</span>
<button (click)="createBillingCard(entity.id)" mat-raised-button class="add-button">ADD</button>
</mdl-card-title>
</mdl-card>
没有什么,如果我在任何 div 上输入 *ngIf="!comapanyHasBilling(entity.id),她会出现...为什么?日志说我得到了 id
编辑:
TS:
public companyHasBilling(companyId: string) {
const searchID = this.billingList.find(item => item.companyId === companyId)
if (searchID !== undefined) {
console.log(searchID.companyId);
}
return searchID;
};
HTML
<mdl-card-title *ngIf="companyHasBilling(entity.id) === null" mdl-card-expand fxLayout="column" fxLayoutAlign="center center" fxLayoutGap="30px">
<span class="company-card-title">Payment Method</span>
<button (click)="createBillingCard(entity.id)" mat-raised-button class="add-button">ADD</button>
</mdl-card-title>
<div *ngIf="companyHasBilling(entity.id) !== null" class="icon-company-title">
<div class="business-icon">
<i class="material-icons md-18">credit_card</i>
</div>
<span class="company-card-title">Credit Card</span>
<button class="" mat-icon-button [matMenuTriggerFor]="menu">
<i class="material-icons edit">mode_edit</i>
</button>
</div>
但还是什么都没有……
【问题讨论】:
-
第一个条件不应该是
*ngIf="comapanyHasBilling(entity.id)"(没有!)吗?另外:comapanyHasBilling应该返回true或false。 -
它没有任何区别,它只是交换
标签: angular angular-ng-if