5

For a select element I'm trying to prevent the value from changing based on some conditions:

<select [ngModel]="selectedPriority" (change)="onPriorityChanged($event)">
  <option *ngFor="let priority of priorities" [ngValue]="priority">{{priority.label}}</option>
</select> 

onPriorityChanged(event) {
  event.preventDefault();
  return false;
}

This is not working. The model is not updated because I use 1-way binding but the selected item in the select changes, even though I use preventDefault.

What is the correct way to achieve this ?

3
  • try this : ` <select [ngModel]="selectedPriority" (change)="onPriorityChanged($event)"> <option *ngFor="let priority of priorities" [value]="priority">{{priority.label}}</option> </select>` Commented Feb 16, 2018 at 10:09
  • sorry but I'm missing your point. How is that cancellable ? Commented Feb 16, 2018 at 11:18
  • It is not cancelable at all. You want to do some hack like setting previous value on change event in your ngModel. Commented Feb 17, 2018 at 5:19

1 Answer 1

5

'change' event is not cancellable: Refer(https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event and https://stackoverflow.com/a/24252333/6848923)

You can reset value in ngModel on change event if you want to prevent the selection for a particular condition.

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

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.