Reputation: 71
So I have two conditions here with some changes in the UI. Is there a way to rewrite it in a better way?
<i *ngIf="measures.length > 0">
<ul *ngFor="let m of measures">
<io-data-selection-row
[text]="m.getDisplayName()"
[id]="m.getId()"
(deleteIconClicked)="removeSelected($event)"
></io-data-selection-row>
</ul>
</i>
<ul *ngIf="measures.length <= 0">
<io-data-selection-row
[strikeThrough]="'strikethrough'"
[text]="selected"
[id]="selected"
(deleteIconClicked)="removeSelected($event)"
></io-data-selection-row>
</ul>
Upvotes: 1
Views: 85
Reputation: 810
Can you add like this and check if it works?
<i>
<ul *ngFor="let m of measures">
<io-data-selection-row
[strikeThrough]="!m && 'strikethrough'"
[text]="m ? m.getDisplayName() : selected"
[id]="m ? m.getId() : selected"
(deleteIconClicked)="removeSelected($event)"
></io-data-selection-row>
</ul>
</i>
Upvotes: 0
Reputation: 57919
Another aproach is, when you get the measures, make a map (I supose you're getting from a service), or use an if, e.g. some like
this.serviceData.getMeasures.pipe(
map((res:any[])=>{
if (!res || res.length<=0)
return [{text:'selected'}] //<--change to fit in your requeriments
return res;
}).subscribe(res=>{
this.measures=res;
})
Upvotes: 0
Reputation: 584
You have few ways to achieve this.
.
<div *ngIf="measures.length > 0; then thenBlock; else elseBlock"></div>
<div>
<ng-template #thenBlock>
<ul *ngFor="let m of measures">
<io-data-selection-row
[text]="m.getDisplayName()"
[id]="m.getId()"
(deleteIconClicked)="removeSelected($event)">
</io-data-selection-row>
</ul>
</ng-template>
<ng-template #elseBlock>
<io-data-selection-row
[strikeThrough]="'strikethrough'"
[text]="selected"
[id]="selected"
(deleteIconClicked)="removeSelected($event)">
</io-data-selection-row>
</ng-template>
</div>
Upvotes: 0
Reputation: 26360
You can use ngIf-else, but I don't think there's a much shorter way:
<i *ngIf="measures.length; else nolength">
<ul *ngFor="let m of measures">
<io-data-selection-row [text]="m.getDisplayName()" [id]="m.getId()"
(deleteIconClicked)="removeSelected($event)"></io-data-selection-row>
</ul>
</i>
<ng-template #nolength>
<ul>
<io-data-selection-row [strikeThrough]="'strikethrough'" [text]="selected" [id]="selected"
(deleteIconClicked)="removeSelected($event)"></io-data-selection-row>
</ul>
</ng-template>
Upvotes: 0
Reputation: 639
Just use *ngIf="measures.length"
for !== 0 and *ngIf="!measures.length"
for === 0
Upvotes: 1