refactor
refactor

Reputation: 15054

Understanding Angular2 change event

I am new to Angular2 , and working on sample to learn the concepts

Below is my partial sample code ,

...
....
<select name="mExp" [(ngModel)]="model.exp"    (change)="expChange($event.target.value)">
 <option *ngFor="let exp of modelExps" [value]="exp.Name">{{exp.Value}}   </option>
</select>
...
...

....
....
expChange(newExp: string) {
  this.model.exp = newExp;
  console.log(this.model);
}
...
....

In brief , when user selects an option in the dropdown , the selected value is assigned to "this.model.exp" , what if i need to cancel the assignment based on a certain value for "newExp".

Upvotes: 2

Views: 472

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657338

You can split the binding

<select name="mExp" [ngModel]="model.exp" (ngModelChange)="$event === 3 ? expChange($event) : null">

Using ngModelChange is usually better because there are timing differences when change (<select> native event) and ngModelChange are emitted which can cause issues when used together with ngModel.

Upvotes: 1

Related Questions