Answer the question
In order to leave comments, you need to log in
Why doesn't Pipe work correctly in Angular2?
The task is simple, it is necessary that only numbers and only less than a certain number are entered into the input. I did it like this:
export class MaxNumber implements PipeTransform{
transform(value, [maxNumber]) {
value = value.replace(/[^\d]+/g,'');
value = value > maxNumber?maxNumber:value;
return value;
}
}
<input type="text" [ngModel]="obj.count | maxNumber:1000" (ngModelChange)="obj.count=$event" />
Answer the question
In order to leave comments, you need to log in
Update angular2 final release:
There is an easier way to achieve the same behavior:
import {Pipe, PipeTransform, WrappedValue} from '@angular/core';
@Pipe({ name: 'maxNumber'})
export class MaxNumber implements PipeTransform{
transform(value, maxNumber) {
value = value.replace(/[^\d]+/g,'');
return WrappedValue.wrap(value > maxNumber ? maxNumber : value);
}
}
// JS has NaN !== NaN
export function looseIdentical(a, b): boolean {
return a === b || typeof a === "number" && typeof b === "number" && isNaN(a) && isNaN(b);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question