Angular2 ngModel change binding on Input type="number"
changing to one-way binding worked for me, and it's cleaner not to fire the change if user cancels too (note I still had to manually update the DOM as you see):
<input type="number" [ngModel]="element.value" // one way binding (change)="onChange($event)" >export class NumFieldComponent { @Input() index; @Input() element; //element.value = 0 onChange($event){ var confirm = confirm("Are you sure about this?") if(confirm){ this.element.value = $event.target.value } else { $event.target.value = 0; //Failed, set the input back to 0 } }}
Not tried but might work
export class NumFieldComponent { @Input() index; @Input() element; //element.value = 0 constructor(cdRef:ChangeDetectorRef) {} onChange($event){ var confirm = confirm("Are you sure about this?") if(confirm){ //True, accept the value } else { this.element.value = 0; this.cdRef.detectChanges(); // <<<=== added //Failed, set the input back to 0 } }}