6

I have a number type input and when I try to change the value with an onChange event, it does not work.

I've tried the same for a text input and it works perfectly.

<input
   type="number"
   [(ngModel)]="element.value" 
   (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){
            //True, accept the value
        } else {
            this.element.value = 0;
            //Failed, set the input back to 0
        }
    }
}

I am new to Angular2 so what am I missing here?

PS. I have seen a similar issue with inputs that take bools

6
  • what's the log? Commented Oct 13, 2016 at 22:04
  • Im not sure what you mean? Commented Oct 13, 2016 at 22:07
  • I couldn't make it work either. What I did was changing my input to type="tel" Commented Oct 13, 2016 at 22:28
  • which is exactly the error? the browser's console launch any error? Commented Oct 13, 2016 at 22:42
  • 1
    no error at all, change event fires just fine and setting the value works fine. The UI just doesnt reflect the value being set from the change function Commented Oct 13, 2016 at 23:26

2 Answers 2

3

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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
        }
    }
}

7 Comments

That worked. Is that a work around for a bug in angular2.0.0 or is there a reason change binding works with type="text" and doesnt with type="number"?
I don't think it's a bug. Perhaps it's a timing issue. There is no execution order specified for multiple event bindings like in your ((ngModel) and (change)). You can also try if it works differently when you use (ngModelChange)="..." instead of (change)="...".
I tried ngModelChange and the event fires on keypress vs blur. In the case of a number field, backspacing the 0 or highlighting and overwritting fires it before you get a chance to enter a value. Still doesnt make sense why type="text" with same exact code works.
I admit it sounds weird. What browser are you using?
Chrome Version 53.0.2785.143 (64-bit) OSX
|

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.