I am new with typescript. Trying to create an abstract class for html form. I want to execute some custom code on form submit. Therefore, assigning a function to this.onsubmit. Problem is, the submit method requires the first parameter to be "this" but I need "this" to access class members as well.
export abstract class AbstractForm {
protected form: HTMLFormElement;
constructor(form: HTMLFormElement) {
this.form = form;
this.form.onsubmit = this.Submit;
}
private Submit(this: HTMLElement, ev: Event) {
if (this.PreSubmissionValidation()) { // Error
// Some code
}
ev.preventDefault();
}
abstract PreSubmissionValidation(): boolean;
abstract OnSuccess(): any;
abstract OnError(): any;
}
this.form.onsubmit = this.Submit.bind(this);? I advise against calling your first parameterthis- very confusing, as it's not the value ofthisin the context ofAbstractForm.