Home > Net >  Angular ngModelChange parameter function different to $event
Angular ngModelChange parameter function different to $event

Time:04-23

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


 <div class='col-sm'>
      <label >Origen</label>
          <div >
              <select [(ngModel)]="dana"   
                 (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:

    <div class='col-sm'>
         <label >Origen</label>
             <div >
                 <select [(ngModel)]="dana"   
                     (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'. Did you mean '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:

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.

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/

CodePudding user response:

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

CodePudding user response:

if you use (ngModelChange)="filterFor($event)" in your function filterFor you received the [value] of the option selected.

if you use 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..')...
}
  • Related