简体   繁体   English

在angular2中修补时如何应用管道以形成控制值

[英]How to apply pipe to form control value while patching in angular2

I am using reactive forms approach of angular 2 in my current application. 我在当前应用程序中使用的是角2的反应形式方法。 i am having many input fields, i am patching the json which i got from server. 我有很多输入字段,我正在修补从服务器获取的json。

now i got a requirement like i have to format the value while displaying and send the actual value while sending. 现在我有一个要求,例如我必须在显示时格式化值并在发送时发送实际值。

eg: input currency fields has to format in currency type with commas and while sending it is just a number. 例如:输入的货币字段必须使用逗号分隔货币类型的格式,而发送时只是一个数字。

How can i achieve this. 我怎样才能做到这一点。

i need both value and display value to the form control. 我需要值和显示值到窗体控件。 .

 <input type="text"  data-test="yearlyRevenue" formControlName="yearlyRevenue" [numberformat]="18" >

    numberformat is my custom directive to format the data

In blog.ngconsultant.io you have an example. blog.ngconsultant.io中 ,有一个示例。

the "key" is use a Hotlistener when a "blur" and a "focus" happened. “键”是在发生“模糊”和“焦点”时使用Hotlistener。 If you have two function "transform" and "parse" you can do 如果您有两个函数“转换”和“解析”,则可以执行

ngOnInit() {
    this.el.value = this.transform(this.el.value);
}

@HostListener("focus", ["$event.target.value"])
onFocus(value) {
   this.el.value = this.parse(value); // opossite of transform
 }

@HostListener("blur", ["$event.target.value"])
  onBlur(value) {
    this.el.value = this.transform(value);
}

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

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