1

I'm looking for a way to bind my number property to an time input. I want to do that because my model properties must only be type number. I would like to convert the time value to seconds with a binding. I tried this code:

<input type="time" step="1" [ngModel]="value.value | timeToNumber" (ngModelChange)="value.value = $event">

My timeToNumber pipe convert string "HH:mm:ss" to seconds. But it doesn't works because the input cannot accept number type. How can I manage to do that ?

1 Answer 1

0

I think you can't do it in this way because kind of an infinite loops of calling ngModelChange after input's element had been changed which will cause calling the pipe "timeToNumber" which will cause ngModelChange to be called again ..

you can solve it by saving one more variable: "value.valueForm" for example which will hold your piped version value of "value.value".

for example:

            <input type="time" step="1" placeholder="time in seconds.." name="myTime" #iTime #myTime="ngModel"
                    [ngModel]="value.valueForm" 
                    (ngModelChange)="value.valueForm=onChange($event, iTime)" />


onChangeCash(eventStr: string, eRef): string {
    var res = this.getNumberOnChange(eventStr, eRef); // gets current value of element using eRef. Pipe it and return new piped value as res.
    this.value.value = this.getNumberOfStr(res); // gets new piped value as res and convert it to Model value you need to save.
    return res;
}

Sorry for not having an example of getNumberOnChange(), getNumberOfStr().

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.