I normally create my forms this way:
export class ModelXFormGroup extends FormGroup {
constructor() {
super({
property1: new FormControl()
});
}
getRawValue(): ModelX {
return super.getRawValue();
}
patchValue(template: ModelX) {
super.patchValue();
}
get property(): FormControl {
return this.get('property') as FormControl;
}
}
If I need to make a property "disabled" depending on other, then in the constructor I can do like:
this.property1.valueChanges(newValue -> {
if(newValue){
this.property2.disabled();
} else{
this.property2.enabled();
}
});
But I don't know how I could I kill those subscriptions... I mean, I cannot use here ngOnDestroy and call this.subscription.unsubscribe().
Any way to do this inside the form?
Because the only alternative I'm thinking is like having a method in the form to unsuscribe manually all properties...
Component:
onDestroy {
this.form.unSuscribe();
}
But I would like to make it transparent for the component..
You could use
takeUntil
If want to use
valueChanges
in the class, maybe we could create a specific subject in the class as a trigger for unsubscriptionlater in the component you could call this
subject
inngOnDestroy