Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit the value displayed by [(ngModel)]?

I am receiving a value, that I display to the user in a text field.

The idea is for him to be able to edit this value, and then send it back.

In the case of money, let's say I store the amount in cents, but I want to display it in dollars. Here is an idea of the code I have that display the value I receive :

<input type="text" [(ngModel)]="myValue" value="{{myValue}}" />

I tried this without success :

<input type="text" [(ngModel)]="myValue/100" value="{{myValue/100}}" />

How can I display that value divided by 100 ?

like image 639
user96649 Avatar asked Oct 15 '25 15:10

user96649


1 Answers

Use the desugared syntax of [()].

<input type=text [(ngModel)]="myValue">

is equivalent to

<input type=text [ngModel]="myValue" (ngModelChange)="myValue = $event">

This means that you can separately control how do you want data to flow in, and how should events flow out.

like image 68
Lazar Ljubenović Avatar answered Oct 17 '25 07:10

Lazar Ljubenović