Reputation: 41
I'm writing a custom angular2 component that using Custom ngModel.
The problem i'm facing is ngModel will be bound after another binding like "@input". For example on the same method, i change value for ngModel and value for @input. Inside the child component, i need to handle if any change happens at ngOnChanges. But that seesm the value for @input has changed, but the value for ngModel is still the old value ( i need to get the new value here for some validation)
Here is demo on plunker:
## https://plnkr.co/edit/U6eSZ6GJB6HvHADLyEG2?p=preview ##
Waiting for some ideas. Thanks.
Upvotes: 2
Views: 932
Reputation: 17889
You can aggregate the changes in one place and do validation there. Create two subjects for model and maximum, push change there and combine the observables for validation.
ngOnChanges(changes: SimpleChanges) {
alert("current model " + this.value + "--- current maxinum " + changes.maximum.currentValue);
this.maximum$.next(this.maximum);
}
//From ControlValueAccessor interface
writeValue(value: any) {
if (value !== this.innerValue) {
this.model$.next(value);
this.innerValue = value;
}
}
Observable.combineLatest(this.maximum$, this.model$).
subscribe(validateHere)
Upvotes: 1