I am trying to put some common code, which adds server errors to the form etc, and would be used in all my form components, in a base form component.
I have simplified the example code to demonstrate my problem.
import { Component, } from '@angular/core';
import { Validators } from '@angular/forms';
import { FormGroup, FormBuilder } from '@angular/forms';
export abstract class BaseFormComponent {
form: FormGroup;
fb = new FormBuilder;
submitted = false;
busy: boolean;
onSubmit() {
this.submitted = true;
this.busy = true;
}
}
@Component({
selector: 'my-form',
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit()" novalidate>
<div class="form-group">
<label>Name</label>
<input formControlName="name" class="form-control">
</div>
<button type="submit" [disabled]="busy" class="btn btn-primary">Submit</button>
</form>
`
})
export class FormComponent extends BaseFormComponent {
constructor() {
super();
this.createForm();
}
protected createForm() {
this.form = this.fb.group({
name: ['', Validators.compose([Validators.required, Validators.maxLength(10)]) ],
});
}
}
For some reason I am getting errors that the template cannot find the base class properties.
Error: [21, 24]: The property "form" that you're trying to access does not exist in the class declaration. [21, 42]: The method "onSubmit" that you're trying to access does not exist in the class declaration. [26, 41]: The property "busy" that you're trying to access does not exist in the class declaration. [21, 24]: The property "form" that you're trying to access does not exist in the class declaration. [21, 42]: The method "onSubmit" that you're trying to access does not exist in the class declaration. [26, 41]: The property "busy" that you're trying to access does not exist in the class declaration.
Comparing my code to this article, it seems pretty much identical except I am not decorating the base form class as a component. Decorating the base class does not seem to make any difference anyway.
I am using: