I am trying to figure out how to use a pipe within a reactive form so that the input is forced into a currency format. I have already created my own pipe for this which I have tested in other areas of the code so I know it works as a simple pipe. My pipe name is 'udpCurrency'
The closest answer I could find on stack overflow was this one: Using Pipes within ngModel on INPUT Elements in Angular2-View However this is not working in my case and I suspect it has something to do with the fact that my form is reactive
Here is all the relevant code:
The Template
<form [formGroup]="myForm" #f="ngForm"> <input class="form-control col-md-6" formControlName="amount" [ngModel]="f.value.amount | udpCurrency" (ngModelChange)="f.value.amount=$event" placeholder="Amount"> </form>
The component
import { Component, OnInit, HostListener } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; export class MyComponent implements OnInit { myForm: FormGroup; constructor( private builder: FormBuilder ) { this.myForm = builder.group({ amount: ['', Validators.required] }); } }
The error:
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined: '. Current value: 'undefined: undefined'
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.
To use pipes within ngModel on input elements in Angular, we can use separate [(ngModel)] into [ngModel] and (ngModelChange) . to apply the useMyPipeToFormatThatValue pipe to item. value in ngModel . Then we handle our input change events in ngModelChange .
The pipe is applying on the formControlName, not on the input text value, so I think it's not related with it. You should have a valid formControlName value at any time, it can't be null, undifined or refer to some fomControl that not exists.
You use data binding with a pipe to display values and respond to user actions. If the data is a primitive input value, such as String or Number , or an object reference as input, such as Date or Array , Angular executes the pipe whenever it detects a change for the input value or reference.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With