简体   繁体   中英

Angular2 pipe not working for input

I'm using angular2 inbuilt pipe percent in my HTML

<input class="ibox1 rightalign" type="text" [ngModel]="_note.StudentPercent| percent:'.5-5'" ngControl="StudentPercent" pattern="^[0-9]\d*(\.\d+)?$" #StudentPercent="ngForm">

its working & display correct data to input box, but when I change the value of a field pipe doesn't work.

How to resolve this?

You have reapply filter on your model value on change of _note.StudentPercent input. You could use ngModelChange handler for the same.

<input class="ibox1 rightalign" type="text" 
   [ngModel]="_note.StudentPercent| percent:'.5-5'" 
   (ngModelChange)="changeToPercent(_note.StudentPercent,'.5-5')" 
   ngControl="StudentPercent" pattern="^[0-9]\d*(\.\d+)?$"
   #StudentPercent="ngForm" />

Code

changeToPercent(percent, format){
   //make sure PercentPipe in declarations & providers of NgModule
   this._note.StudentPercent = new PercentPipe().transform(percent, format)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM