3

I am currently working on a small angular project and have a problem with input events.

I have following code:

<select (change)="onSelect($event.target.value)" >   
    <option *ngFor="let option of options" [value]="option.id">{{option.name}}</option> 
</select>

This is working fine and well. But I wanted to change it to following:

<select (change)="onSelect($event.target.value)" [(ngModel)]="selectedItem">
    <option *ngFor="let option of options" [value]="option.id">{{option.name}}</option>
</select>

Now after this the change event is not triggered anymore. I also tried [ngModel] and (ngModelChange) and a lot more. But as long as there is something with ngModel and a binding in it the events do not trigger anymore.

All my Angular stuff is in version 5.1.2, the CLI in 1.6.2

Does anyone know why Angular has that behaviour? Greetings and thank you.

1
  • I will join the call for this question, the problem I faced was with ngModel, in AOT compilation (using anuglar ng build). It refuses to pick up the event change. Commented Dec 31, 2017 at 8:00

3 Answers 3

1

it could work like this:

<select type="number" [(ngModel)]="selectedItem" >
  <option *ngFor="let option of options" [ngValue]="option.id">{{option.name}}</option>
</select>

Here is working plnkr hope it solve your issue bro :)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. But I would still like to have the onChange trigger.
you still able to have, plnkr updated ... check it out again plz
But exactly that is what is not working in my project. [(ngModel)] and a (ngModelChange) together do somehow not work.
0

You should use ngModelChange when you are binding value using ngModel

<select (ngModelChange)="onSelect($event.target.value)" [(ngModel)]="selectedItem">
    <option *ngFor="let option of options" [value]="option.id">{{option.name}}</option>
</select>

5 Comments

just try with the above [(ngModel)]
That is what I meant. I tried all of the possible combinations of them.
No, that is the funny thing. It works on dmeo and stackblitz. It does however not work in my project. Is there anything you can think of why that might be?
I also tried it in a local new project. There it also works. Only in my project, that is admittedly a bit bigger, it does not work.
what is the version
0

You can try this:

<select type="number" [(ngModel)]="selectedItem" >
  <option *ngFor="let option of options" (click)=“selectedItem(option)” [ngValue]="option.id">{{option.name}}</option>
</select>

In component.ts -

optionSelected: any;
selectedItem(option: any) {
if (option) {
 this.optionSelected = option;
} else { 
 this.optionSelected = ‘’;
}
}

Then you can use this.optionSelected according to your need..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.