简体   繁体   English

Angular ngModelChange 参数 function 与 $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>

I would like to send the parameter in the function filterFor call:我想在 function 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>

Fails:失败:

error TS2551: Property 'dano' does not exist on type 'ComunidadFiltracionesComponent'.错误 TS2551:属性“dano”在类型“ComunidadFiltracionesComponent”上不存在。 Did you mean 'danos'?您指的是 'danos' 吗? . .

Do you know the format of the parameter so that it accepts it?你知道参数的格式以便它接受吗? Thanks in advance提前致谢

Expand the query:展开查询:

I have an object with different parameters:我有一个具有不同参数的 object:

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

In dropdown (ngFor), when the customer selects the make of the car, we take the comment variable.在下拉列表 (ngFor) 中,当客户选择汽车品牌时,我们采用评论变量。

If I want to compare vehicles:如果我想比较车辆:

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

To find out which brand the client has entered, I have to compare through the comment (too long).要找出客户输入的是哪个品牌,我必须通过评论(太长)进行比较。 I want to compare by the brand variable:我想通过品牌变量进行比较:

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

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

if you use (ngModelChange)="filterFor($event)" in your function filterFor you received the [value] of the option selected.如果您在 function filterFor 中使用(ngModelChange)="filterFor($event)" ,您会收到所选选项的 [value]。

if you use as value [value]="dano.make"如果你使用 as value [value]="dano.make"

You can use some like你可以使用一些像

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..')...
}

the issue in your code is that your'e trying to access dano variable which is out of the loop scope and hence isn't being recognized您代码中的问题是您试图访问循环 scope 之外的 dano 变量,因此未被识别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM