I'm using this statement in my html template:
[(ngModel)]="tempProduct.unitprice | number : '1.2-2'"
But when i run it im getting this error in console:
Cannot have a pipe in an action expression...
I need to use this number pipe but with [(ngModel)]
or i will not get data. Any suggestion how can i fix this?? I tried with [ngModel]
but when i do that i dont get data, its empty in html template.
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 .
A pipe takes in data as input and transforms it into a different shape for displaying. Pipes are simple functions to use in template expressions to accept an input value and return a transformed value. Pipes are useful because you can use them throughout your application, while only declaring each pipe once.
Steps Involved In Creating a Custom Pipe In Angular are: 1) Create a Pipe Class and decorate it with the decorator @Pipe. 2) Supply a name property to be used as template code name. 3) Register your Pipe in the module under declarations. 4) Finally, implement PipeTransform and write transformation logic.
Perhaps this should work for you:
[ngModel]="tempProduct.unitprice | number : '1.2-2'" (ngModelChange)="tempProduct.unitprice = $event"
This way "two-way"-binding is split in property-binding and event binding which allows more complex expressions.
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