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

2019-05-19 06:17发布

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 ?

1条回答
戒情不戒烟
2楼-- · 2019-05-19 06:59

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.

查看更多
登录 后发表回答