How to use pipes in Angular 5 reactive form input How to use pipes in Angular 5 reactive form input angular angular

How to use pipes in Angular 5 reactive form input


This is what can happen when you mix template driven form and reactive form. You have two bindings fighting each other. Choose either template driven or reactive form. If you want to go the reactive route, you can use [value] for your pipe...

Note, this pipe is rather only for showing the desired output in the view.

<form [formGroup]="myForm">  <input     [value]="myForm.get('amount').value | udpCurrency"    formControlName="amount"     placeholder="Amount"></form>


I thought I had this working but as it turns out, I was wrong (and accepted a wrong answer). I just redid my logic in a new way that works better for me and answers the concern of Jacob Roberts in the comments above. Here is my new solution:

The Template:

<form [formGroup]="myForm">  <input formControlName="amount" placeholder="Amount"></form>

The Component:

import { Component, OnInit, HostListener } from '@angular/core';import { FormBuilder, FormGroup, Validators } from '@angular/forms';import { UdpCurrencyMaskPipe } from '../../../_helpers/udp-currency-mask.pipe';export class MyComponent implements OnInit {  myForm: FormGroup;  constructor(    private builder: FormBuilder,    private currencyMask: UdpCurrencyMaskPipe,  ) {    this.myForm = builder.group({      amount: ['', Validators.required]    });    this.myForm.valueChanges.subscribe(val => {      if (typeof val.amount === 'string') {        const maskedVal = this.currencyMask.transform(val.amount);        if (val.amount !== maskedVal) {          this.myForm.patchValue({amount: maskedVal});        }      }    });  }    }

The Pipe:

import { Pipe, PipeTransform } from '@angular/core';@Pipe({    name: 'udpCurrencyMask'})export class UdpCurrencyMaskPipe implements PipeTransform {    amount: any;    transform(value: any, args?: any): any {        let amount = String(value);        const beforePoint = amount.split('.')[0];        let integers = '';        if (typeof beforePoint !== 'undefined') {            integers = beforePoint.replace(/\D+/g, '');        }        const afterPoint = amount.split('.')[1];        let decimals = '';        if (typeof afterPoint !== 'undefined') {            decimals = afterPoint.replace(/\D+/g, '');        }        if (decimals.length > 2) {            decimals = decimals.slice(0, 2);        }        amount = integers;        if (typeof afterPoint === 'string') {            amount += '.';        }        if (decimals.length > 0) {            amount += decimals;        }        return amount;    }}

Now there are several things i learned here. One was what that what Jacob said was true, the other way only worked initially but would not update when the value had changed. Another very important thing to note was that I need a completely different type of pipe for a mask as compared to a view pipe. For example, a pipe in a view might take this value "100" and convert it to "$100.00" however you would not want that conversion to happen as you are typing the value, you would only want that to happen after were done typing. For this reason i created my currency mask pipe which simply removes non numeric numbers and restricts the decimal to two places.


I was going to write a custom control, but found that overriding the "onChange" from the FormControl class via ngModelChange was easier. The emitViewToModelChange: false is critical during your update logic to avoid recurring loop of change events. All piping to currency happens in the component and you don't have to worry about getting console errors.

<input matInput placeholder="Amount"   (ngModelChange)="onChange($event)" formControlName="amount" />
@Component({  providers: [CurrencyPipe]})export class MyComponent {  form = new FormGroup({    amount: new FormControl('', { validators: Validators.required, updateOn: 'blur' })  });  constructor(private currPipe:CurrencyPipe) {}  onChange(value:string) {    const ctrl = this.form.get('amount') as FormControl;    if(isNaN(<any>value.charAt(0))) {      const val = coerceNumberProperty(value.slice(1, value.length));      ctrl.setValue(this.currPipe.transform(val), { emitEvent: false, emitViewToModelChange: false });    } else {      ctrl.setValue(this.currPipe.transform(value), { emitEvent: false, emitViewToModelChange: false });    }  }  onSubmit() {    const rawValue = this.form.get('amount').value;    // if you need to strip the '$' from your input's value when you save data    const value = rawValue.slice(1, rawValue.length);    // do whatever you need to with your value  }}