Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

angular - include message when filter by radio input does not show matches

I have an array with a series of elements that correspond to 3 animals. I have "n" radio input to filter these animals. When there are no coincidences, nothing will be shown, then I want to show a message when there are no coincidences. I do not know how to have an optimal solution and a maintainable code. I can have "n" amount of input radios How can I do it?

<input type="radio" name="radio_option" [(ngModel)]="radio_option" value='all'> show all
<input type="radio" name="radio_option" [(ngModel)]="radio_option" value='horse'> Horse
<input type="radio" name="radio_option" [(ngModel)]="radio_option" value='chicken'> Chicken
<input type="radio" name="radio_option" [(ngModel)]="radio_option" value='cat'> Cat
<input type="radio" name="radio_option" [(ngModel)]="radio_option" value='dog'> dog
<input type="radio" name="radio_option" [(ngModel)]="radio_option" value='cow'> cow
<input type="radio" name="radio_option" [(ngModel)]="radio_option" value='bird'> bird
....
<ng-container *ngFor="let item of options">
    <p *ngIf="radio_option==item.animal || radio_option=='all'">
          {{item.animal}}
    </p>
</ng-container>

  options:any=[
    {
      "animal":"horse"
    },
    {
      "animal":"chicken"
    },
    {
      "animal":"cat"
    },
    {
      "animal":"horse"
    },
    {
      "animal":"chicken"
    },
    {
      "animal":"cat"
    }    
  ]
}

https://stackblitz.com/edit/angular-xphzkm?file=src/app/app.component.ts

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the solution:

<div [ngSwitch]="radio_option">
   <ng-container *ngFor="let item of options"> 
      <p *ngSwitchCase="item.animal">
        {{item.animal}}
      </p>
      <p *ngSwitchCase="'all'">
        {{item.animal}}
      </p>
   </ng-container>
   <p *ngSwitchDefault>
    No items found
   </p>
</div>

Updated Your code with solution: https://stackblitz.com/edit/angular-9ywf9d


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...