Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select change event occurs before ngModel updates #7971

Closed
brtnshrdr opened this issue Apr 8, 2016 · 13 comments
Closed

Select change event occurs before ngModel updates #7971

brtnshrdr opened this issue Apr 8, 2016 · 13 comments

Comments

@brtnshrdr
Copy link

I'm trying to get the updated select box value, which in this case is an object, when it changes. I'm trying to use the [ngValue]="object" syntax added in #7842 like so

<select [(ngModel)]="selectedObj" (change)="objChanged($event)">

<option *ngFor="#obj of searchResults" [ngValue]="obj">
    {{obj.name}}
</option>

</select>

The $event.target.value is a string equal to "1: Object" (The "1" is the index of the array I assume)
However, if in that same method I print out "selectedObj", it's always the old value. It seems as though the change event fires before the model is updated.

edit - If ngFor redraws the array of option values, the index keeps incrementing, so you can't use it to pull the correct object from the array ("searchResults" in this case)

Steps to reproduce and a minimal demo of the problem

http://plnkr.co/edit/7rIHLBWWVqpcPL1NrpDz?p=preview

What steps should we try in your demo to see the problem?

If you look at the console.log()'s, you'll notice that the "Selected Obj: " is always equal to the old value when you select a new value

Expected/desired behavior

I would like either the model object to be updated before the change event fires, or a way to access the new value from the $event obj passed into the change function.

@samridh90
Copy link

I have the same issue. I wanted to use the new value when the select option changed. Here's the workaround I used:
In the component class

private _selectedObj;
get selectedObj() {
    return this._selectedObj;
}
set selectedObj(newObj) {
    this._selectedObj = newObj;
    this.objChanged(newObj);
}

ngOnInit(): void {
    this._selectedObj = this.getDefaultValue();
}

In the template

<select [(ngModel)]="selectedObj">
<option *ngFor="#obj of searchResults" [ngValue]="obj">
    {{obj.name}}
</option>
</select>

@zoechi
Copy link
Contributor

zoechi commented Apr 8, 2016

I think it's the same as #7822 and caused by #6311

@kleinech
Copy link

You can use (ngModelChange) instead of (change):

<select [(ngModel)]="selectedObj" (ngModelChange)="objChanged($event)">

<option *ngFor="#obj of searchResults" [ngValue]="obj">
    {{obj.name}}
</option>

</select>

@pkozlowski-opensource
Copy link
Member

(ngModelChange) is the way to go

@pawantiwari1
Copy link

you can also used change method
In html file
<select [(ngModel)]="selectedObj" (change)="selectChangeHandler($event)">

{{obj.name}}

in ts file
selectChangeHandler(event:any)
{
this.selectedshift=event.target.value;
console.log("if change in method "+this.selectedshift)
}

@nsivaji61
Copy link

This code is not working prod mode. Any suggestion?

@mlc-mlapis
Copy link
Contributor

mlc-mlapis commented Dec 29, 2017

@nsivaji61 ... it has to be ngModelChange as ... <input [ngModel]="username" (ngModelChange)="...">

@nsivaji61
Copy link

I want to fire onchange event when ever user selects anything because i have a cascading dropdown lists. Please suggest.

Thanks

@vishbal
Copy link

vishbal commented Apr 25, 2018

nsivaji61 : Were you able to resolve the issue ? I have same problem of updating cascading drop downs and (change)="somemethod()" is triggered without any user selection change.

@pawantiwari1
Copy link

You can build as ng build command then its work in my project

@ashfaq-unstoppable
Copy link

sample ngValue use

@ishwara-bhat
Copy link

this.selectedshift=event.target.value;

With angular 8, I saw that event.value works.
console.log(event) gives me-
MatSelectChange {source: MatSelect, value: "ffff"}

Best recomemndation is do console.log of the event object, if nothing is working. Then tune your code.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests