Hi
I am getting error - Property 'appDateCompare' has no initializer and is not definitely assigned in the constructor.
@Directive({
selector: '[appDateCompare]',
standalone: true,
providers: [
{
provide: NG_VALIDATORS,
useExisting: DateCompareDirective,
multi: true
}
]
})
export class DateCompareDirective implements Validator {
@Input('appDateComparison') appDateCompare: string; // FormControlName for DOB
validate(control: AbstractControl): ValidationErrors | null {
const dtStart0 = control.root.get(this.appDateCompare);
if (!dtStart0 || !dtStart0.value || !control.value) {
return null;
}
const dtStart = new Date(dtStart0.value);
const dtEnd = new Date(control.value);
if (dtEnd <= dtStart) {
return { 'dateComparison': true };
}
return null;
}
}
Start Date
End Date
@if (sessionForm.get('endDate')?.hasError('required')|| sessionForm.get('endDate')?.touched) {
{{displayErrMessage.required}}
}
Thanks
Jayraj ChhayaPosted Jun 28, 2024, 7:14 AM
To resolve the "Property 'appDateCompare' has no initializer" error in your Angular directive, you need to ensure that the property
appDateCompareis initialized in the constructor or assigned a default value. Here's how you can modify yourDateCompareDirectiveclass:By providing an initializer for the
appDateCompareproperty, you can avoid the error and ensure that the property is properly initialized when the directive is used.