2

I am binding input element with model property in angular 5

<input [(ngModel)]="MB.YearOfOperation | date: 'dd-MMM-yyyy' " type="text" class="form-control">

Using date pipe to format its value but it gives error

Cannot have a pipe in an action expression at column 33

so I tried below approach with (ngModelChange)

<input [(ngModel)]="MB.YearOfOperation | date: 'dd-MMM-yyyy' " (ngModelChange)="MB.YearOfOperation =$event" type="text" class="form-control">

But still it give the same error , How can I use pipes with [(ngModel)] ??

Tanwer
  • 1,503
  • 8
  • 26
  • 41
  • 1
    Possible duplicate of [Using Pipes within ngModel on INPUT Elements in Angular](https://stackoverflow.com/questions/39642882/using-pipes-within-ngmodel-on-input-elements-in-angular) – Sivakumar Tadisetti Aug 11 '18 at 07:37

1 Answers1

5

You should not use pipe with two way databinding, if you really want to use it with ngModel, you should consider one way data binding with ngModelChange as follows,

[ngModel]="MB.YearOfOperation | date: 'dd-MMM-yyyy'" (ngModelChange)="updateDate($event)"
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • No , I wrote a function updateDate() to return Date value in required format but MB.YearOfOperation is going as null in updateDate() – Tanwer Aug 13 '18 at 04:22