为了账号安全,请及时绑定邮箱和手机立即绑定

按药丸/按钮过滤,而不是使用选择 - 角度

按药丸/按钮过滤,而不是使用选择 - 角度

12345678_0001 2021-10-29 16:45:47
我正在使用 Angular 进行构建,并使用过滤器管道从*ngFor循环的下拉列表中过滤选定的选项。然后相应地过滤内容。我想将选择选项交换为按钮或药丸。所以当一个按钮被点击时,过滤器就会发生——这个按钮将作为一个开/关开关,所以你可以过滤多个选项。这是我的 stackblitz 示例 - https://stackblitz.com/edit/timeline-angular-7-tyle1f    <div class="form-group row">      <div class="col-sm">        <select class="form-control" name="locationFilter" id="locationFilter" [(ngModel)]="filteredLocation">          <option value="All">All</option>          <option *ngFor="let entry of timeLine | filterUnique" value="{{entry.location}}">{{entry.location}}          </option>        </select>      </div>//Button filters below       <div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">            <button class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">{{entry.location}}</button>       </div>    </div>我不知道如何让按钮以相同的方式工作,因为没有过滤发生......任何帮助表示赞赏!?
查看完整描述

3 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

(click)="filteredLocation = entry.location"给按钮绑定一个点击事件


像这样尝试:


 <div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">

     <button  (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">{{entry.location}}</button>

  </div>


查看完整回答
反对 回复 2021-10-29
?
红颜莎娜

TA贡献1842条经验 获得超13个赞

关键是将按钮/开关的值分配给您的filteredLocation变量,如下所示

<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">{{entry.location}}</button>

希望这可以帮助 :)


查看完整回答
反对 回复 2021-10-29
  • 3 回答
  • 0 关注
  • 262 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号