1
私はカスタムパイプで双方向データバインディングを持っています。パイプ、ngModelとngModelChange
私の入力テキストフィールドコード:
<input type="text" [ngModel]="post.price | myCurrencyPipe" (ngModelChange)="post.price = $event" name="price" id ="price" (change)="itemPriceUpdate(post)"/>
とカスタムパイプコード:
@Pipe({ name: 'myCurrencyPipe'})
export class MyCurrencyPipe implements PipeTransform{
transform(val:any) {
var value = parseInt(data);
var num = '$' + value.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
return num;
}
私はそれがキー入力にはNaNを表示する私の入力フィールドの値を編集するために行きます。
ご協力ありがとうございます。私はあなたの指示に従って努力しました。入力フィールドにはまだNANの問題がありました。 これで、カスタムパイプに余分な条件を追加しました。入力フィールドにnanが表示されます。 if(isNaN(val)){ var num1 = ''; return num1; } –
'parseInt'の@SalmaKonaパラメータは' number'の型でなければなりません。そうでなければ 'NAN'に変換されます。 – Pengyy
ありがとう@Pengyyあなたの助けになりました。 –