Angular筛选表单
创始人
2024-10-29 09:30:41
0

下面是一个示例,展示了如何使用Angular进行筛选表单:


  • {{ product.name }} - {{ product.category }}
// 在组件的类中
import { Component } from '@angular/core';

@Component({
  selector: 'app-products',
  template: `
    
  `,
  styleUrls: ['./products.component.css']
})
export class ProductsComponent {
  products: any[] = [
    { name: 'iPhone', category: '电子产品' },
    { name: 'Sofa', category: '家具' },
    { name: 'T-shirt', category: '服装' },
    // 其他产品
  ];
  filteredProducts: any[] = [];
  filter: any = {};

  filterProducts() {
    this.filteredProducts = this.products.filter((product) => {
      let nameMatch = true;
      let categoryMatch = true;

      if (this.filter.name) {
        nameMatch = product.name.toLowerCase().includes(this.filter.name.toLowerCase());
      }

      if (this.filter.category) {
        categoryMatch = product.category === this.filter.category;
      }

      return nameMatch && categoryMatch;
    });
  }
}

在上面的示例中,我们首先在模板中创建了一个表单,包含一个输入框和一个下拉菜单,用于输入筛选条件。然后,我们使用ngModel指令将输入框和下拉菜单的值与组件中的filter对象进行绑定。当用户点击"筛选"按钮时,会调用filterProducts方法。

filterProducts方法使用Array.filter函数对产品数组进行筛选。根据filter对象中的值进行匹配,如果产品名称中包含输入的名称,并且产品类别与选择的类别匹配,就会被保留在filteredProducts数组中。最后,我们使用*ngFor指令在页面上循环渲染筛选后的产品列表。

注意:以上示例仅供参考,实际应用中,您可能需要根据自己的需求进行相应的修改。

相关内容

热门资讯

一分钟教你!wepoker私人... 一分钟教你!wepoker私人局辅助挂,来玩app 德州 辅助,曝光教程(有挂攻略);wepoker...
透视辅助"wepok... 1、透视辅助"wepoker可以透视码"详细辅助德州论坛(从来真的是有挂)(UU poker、wep...
盘点一款!安装不了wepoke... 此外,数据分析德州(wepoker插件程序激活码)辅助神器app还具备辅助透视行为开挂功能,通过对客...
透视黑科技"购买we... 《透视黑科技"购买wepoker模拟器"详细辅助规律教程(素来是有挂)》 购买wepoker模拟器软...
推荐一款!hhpkoer辅助器... 推荐一款!hhpkoer辅助器视频,wepoker破解版内购,AI教程(有挂技巧);致您一封信;亲爱...
透视总结"wepok... 透视总结"wepoker透视脚本视频"详细辅助力荐教程(原生存在有挂);建议优先通过wepoker透...
透视玄学"wepok... 这是一款非常优秀的wepoker怎么设置房间 ia辅助检测软件,能够让你了解到wepoker怎么设置...
分享开挂内幕!德州局透视脚本免... 分享开挂内幕!德州局透视脚本免费版下载手机版,wepoker私人局俱乐部,2025版教程(有挂方法)...
透视挂透视"aapo... 透视挂透视"aapoker辅助器怎么用"详细辅助爆料教程(往昔有挂)这是由厦门游乐互动科技有限公司精...
信息共享!hhpoker辅助,... 信息共享!hhpoker辅助,wepoker有没有插件,德州论坛(有挂软件);《WPK辅助透视》‌:...