Naveen
Naveen

Reputation: 111

Angular 2 How to use pipes on input field in case of two way binding

I know this is similar or duplicate to this , I want to use pipe on ngmodel like this [ngModel]="item.value | currency:'USD$'" but i have a scenario where I need to bind a method like this (ngModelChange)="someMethod()" instead of setting the value (ngModelChange)="item.value=$event". How to achieve both of them together.

My Code:

<input type="text" [(ngModel)]="item.value | currency:'USD$'"  (ngModelChange)="someMethod()" />

Upvotes: 0

Views: 4502

Answers (3)

Brian Davis
Brian Davis

Reputation: 817

Drop the parenthesis like so, and pass the event to your method.

<input type="text" [(ngModel)]="item.value | currency:'USD$'"  (ngModelChange)="someMethod()" />

Then in your .ts file you can assign the value:

someMethod(event) {
    item.value = event;
    //Other stuff you were doing before
}

Upvotes: 0

MrKnyaz
MrKnyaz

Reputation: 190

Try like this

<input type="text" [ngModel]="item.value | currency:'USD$'"  (ngModelChange)="someMethod($event)" />

Upvotes: 1

Gorsky
Gorsky

Reputation: 13

Try removing square bracket from ngModel and see if that helps. It worked for me.

Upvotes: 0

Related Questions