Angular ngModelChange 参数函数与 $event 不同

Angular ngModelChange parameter function different to $event

I want to send a different parameter to the $event in the function:


 <div class='col-sm'>
      <label class="col-3 col-form-label">Origen</label>
          <div class="col-4">
              <select [(ngModel)]="dana" class="form-control"  
                 (ngModelChange)="filterFor($event)"required>
                   <option *ngFor="let dano of danos" 
                       [value]="dano.comment">{{dano.make}}
                   </option>
             </select>
          </div>
     </div>

我想在函数 filterFor 调用中发送参数:

    <div class='col-sm'>
         <label class="col-3 col-form-label">Origen</label>
             <div class="col-4">
                 <select [(ngModel)]="dana" class="form-control"  
                     (ngModelChange)="filterFor(dano.tipo)"required>
                   <option *ngFor="let dano of danos" 
                        [value]="dano.comment">{{dano.make}}
                   </option>
                 </select>
             </div>
       </div>

失败:

错误 TS2551:属性 'dano' 在类型 'ComunidadFiltracionesComponent' 上不存在。您是说 'danos' 吗? .

你知道参数的格式以便它接受吗? 提前致谢

展开查询:

我有一个具有不同参数的对象:

let car = [ {'make': 'Ford', 'comment': 'The vehicle has a heavy internal combustion engine....'}];

在下拉列表 (ngFor) 中,当客户选择汽车品牌时,我们采用评论变量。

如果我想比较车辆:

if (dana == 'The vehicle has a heavy internal combustion engine....'){
this.quality = 'goog';
}

要知道客户输入的是哪个品牌,我必须通过评论进行比较(太长了)。我想通过品牌变量进行比较:

if (dana == 'Ford'){
this.quality = 'goog';
}

在 stackblitz 中查看:https://angular-ivy-mu5mrh.stackblitz.io/

您代码中的问题是您试图访问超出循环范围的 dano 变量,因此未被识别

如果您在函数过滤器中使用 (ngModelChange)="filterFor($event)" 因为您收到了所选选项的 [值]。

如果您用作值 [value]="dano.make"

你可以使用类似的东西

filterFor(value:any){
   this.data=value; //<--if not use [(ngModel)] else [ngModel]
                    //don't forget equal the variable to value

   const dano=this.datos.find(x=>x.make==value)
   console.log(dano) //<--here you has the whole object
   //you can, e.g.
   if (value=='Ford')....
   //or
   if (dano.comment=='The vehicle has a heavy internal..')...
}