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指令在页面上循环渲染筛选后的产品列表。

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

相关内容

热门资讯

透视透视!pokemmo脚本手... 透视透视!pokemmo脚本手机版,微新欢乐卡五星辅助,AI教程(有挂辅助);微新欢乐卡五星辅助辅助...
透视计算!德州私人局脚本,功夫... 透视计算!德州私人局脚本,功夫川麻bug,攻略教程(有挂解说);1、功夫川麻bug系统规律教程、功夫...
透视黑科技!哈糖大菠萝能开挂吗... 透视黑科技!哈糖大菠萝能开挂吗,欢乐达人正版脚本,透明挂教程(有挂插件)1、不需要AI权限,帮助你快...
透视模拟器!wepoker代打... 透视模拟器!wepoker代打辅助,雀神麻将小程序辅助软件,细节揭秘(有挂方法)1、游戏颠覆性的策略...
透视透视!fishpoker透... 透视透视!fishpoker透视,拱趴大菠萝玩的是运气吗,扑克教程(有挂解密)1、每一步都需要思考,...
透视最新!aapoker安装包... 透视最新!aapoker安装包怎么使用,一起宁德福鼎打炸辅助开挂,扑克教程(有挂插件);1、这是跨平...
透视有挂!hhpoker可以开... 透视有挂!hhpoker可以开挂吗,蜂娱辅助,技巧教程(有挂教程);1、超多福利:超高返利,海量正版...
透视辅助!wepoker透视脚... 透视辅助!wepoker透视脚本苹果版,闽游十三水辅助,安装教程(有挂攻略);运闽游十三水辅助辅助工...
透视存在!fishpoker透... 透视存在!fishpoker透视,拱趴大菠萝辅助神器,技巧教程(有挂黑科技);暗藏猫腻,小编详细说明...
透视免费!wepoker私人局... 透视免费!wepoker私人局俱乐部,佛手在线十三道辅助器,详细教程(有挂介绍);1、操作简单,无需...