简体   繁体   English

如何在ngfor中使用if语句?

[英]How can i use the if statement in ngfor?

I have a loop of 5 buttons and I want to disable the second button, so I tried with using the *ngIf inside the *ngFor but its not working correctly for me. 我有5个按钮的循环,我想禁用第二个按钮,因此我尝试在*ngIf使用*ngFor但对我来说无法正常工作。 Can you help me? 你能帮助我吗? Thanks! 谢谢!

<div *ngFor="let day of days">
  <div *ngIf="day == dayFinished ">
    <ion-button id={{day}} expand="block" size="large (click)="test(day)"ngDefaultControl [(ngModel)]="days"  disabled >DAY {{day}}</ionbutton>
  </div>
</div>

You can try this 你可以试试这个

If you want to disable the second button in a loop then you can set i==1 (because index always start from 0) 如果要禁用循环中的第二个按钮,则可以设置i==1 (因为索引始终从0开始)

<div *ngFor="let day of days; let i=index">
<div *ngIf="day == dayFinished ">
  <ion-button id={{day}} expand="block" size="large (click)="test(day)" ngDefaultControl [(ngModel)]="days" [disabled]="i==1" >DAY {{day}}</ionbutton>
</div>

I hope this will be useful 我希望这会有用

You can use index with ngFor . 您可以将索引与ngFor一起ngFor

Here is the way you can do 这是你可以做的方法

<div *ngFor="let day of days; let i=index">
<div *ngIf="day == dayFinished ">
  <ion-button id={{day}} expand="block" size="large (click)="test(day)"ngDefaultControl [(ngModel)]="days"  [disabled]="i==1" >DAY {{day}}</ionbutton>
</div>

index always start from 0 so check i==1 index总是从0开始,所以检查i==1

You can use function in ngFor instead of array like this 您可以在ngFor中使用函数,而不是像这样的数组

in your html=> 在您的html =>中

<div *ngFor="let day of filterDays()">
  <ion-button id={{day}} expand="block" size="large (click)="test(day)"ngDefaultControl [(ngModel)]="days" disabled >DAY {{day}}</ionbutton>

in your component=> 在您的组件中=>

filterDays(){
  return days.filter(x => x.day == "dayFinished");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM