Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pipes in Angular 5 reactive form input

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' 
like image 860
Dallas Caley Avatar asked Mar 27 '18 21:03

Dallas Caley


People also ask

How do you use a pipe in 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.

Can I use pipe in input Angular?

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 .

Can we use pipe in formControlName?

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.

How do pipes work in Angular?

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.


Video Answer


1 Answers

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> 
like image 123
AT82 Avatar answered Sep 17 '22 04:09

AT82