简体   繁体   English

使用NgModel基于绑定变量更改输入字段中显示的值

[英]Change value displayed in input field based on binded variable with NgModel

Basically I have an input tag in my template : 基本上,我的模板中有一个输入标签:

<input (click)="clickableButton(pricerule.id)" type="number" nbInput status="info" shape="rectangle" [(ngModel)]="pricerule.price" placeholder="Price">

pricerule.price is a number, it's a price in centimes (french cents), not in euros (french dollars). pricerule.price是一个数字,它是一个以厘米(法国分)表示的价格,而不是欧元(法国美元)。 For example, if pricerule.price is equal to 3000 , it means 30 dollars. 例如,如果pricerule.price等于3000 ,则表示30美元。

[(ngModel)]="pricerule.price" this part make the bind between the price and the input field. [(ngModel)]="pricerule.price"这部分在价格和输入字段之间建立绑定。 So when the page is loaded, the input field has a default value of pricerule.price ( 3000 for example ). 因此,在加载页面时,输入字段的默认值是pricerule.price (例如3000)。

But I don't want 3000 to be displayed in my input field, my objective is 30 to be displayed, but I have no idea how to do that since I am new about Typescript code. 但是我不希望在输入字段中显示3000,我的目标是显示30,但是我不知道该怎么做,因为我是Typescript代码的新手。

You can't bind one property using ngModel and then display another. 您不能使用ngModel绑定一个属性,然后显示另一个属性。 I would bind the value you want (30) and then converted when you need to do something with it. 我将绑定您想要的值(30),然后在需要使用它进行转换时将其转换。 Or the other way around, convert it first, then bind it. 或反之,先将其转换,然后进行绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM