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

我只是一个虾纸丫 提交于 2020-01-02 06:54:21

问题


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:


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.



来源:https://stackoverflow.com/questions/45832439/how-to-edit-the-value-displayed-by-ngmodel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!