In AngularJs I used to use the ng-submitted class for my form validation but it does not appear in Angular when you submit a form. I have written a directive that adds a class submitted to a form and removes it on reset
@Directive({
selector: '[appForm]'
})
export class FormDirective implements OnDestroy {
private subscription: Subscription;
constructor(el: ElementRef, form: NgForm) {
this.subscription = form.ngSubmit.subscribe(() => {
el.nativeElement.classList.add('submitted');
});
form.onReset = () => {
el.nativeElement.classList.remove('submitted');
};
}
ngOnDestroy() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
}
This works great when you reset the form with a reset button. The problem I am having is when I need to reset with values other than setting all the bindings to null I need to reset the form with the ngForm resetForm method but this does not trigger the onReset method and I can't find a way to tap into the form being reset this way.