代码之家  ›  专栏  ›  技术社区  ›  Paco Zevallos

从角度中的选项选择执行函数

  •  1
  • Paco Zevallos  · 技术社区  · 7 年前

    如何从select选项执行此函数?我正在使用ngFor迭代每个选项,但是根据每个选择,您必须执行函数。

    <select>
      <option *ngFor="let categoria of categoriasTest | async">{{ categoria.nombre }}</option>
    </select>
    

    我是这样做的:

    (click)="filtrarData(categoria.nombre)"
    

    filtrarData(categoria: Categoria) {
      this.avisosTest = this.fs.filterBy(categoria);
      this.selectedCategoria = categoria;
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Sajeetharan    7 年前

    你可以用 ngModelChange公司 ngModel开启时 select

    <select [(ngModel)]="selectedcat" (ngModelChange)="filtrarData()">
      <option *ngFor="let categoria of categoriasTest | async">{{ categoria.nombre }}</option>
    </select>
    

    在TS中

    filtrarData() {
     //DO WHATEVER WITH this.selectedcat;
    }
    
        2
  •  0
  •   Paco Zevallos    7 年前

    最后它还是这样对我有效:

    <select [(ngModel)]="selectedCategoria" (ngModelChange)="filtrarData(selectedCategoria)">
       <option *ngFor="let categoria of categoriasTest | async" >{{ categoria.nombre }}</option>
    </select>
    

    https://stackblitz.com/edit/firestore-filter

    推荐文章