In a form in my Angular app, I am trying to display a div only if a certain condition is met.
When the page loads initially, there are no errors appearing in the console. And the below div is not appearing (as the ngIf is not true).
But, when I submit my form & reset() the form I am getting the following error:
ERROR TypeError: Cannot read property 'value' of null
Here is the code where the error is appearing:
<div class="axis-form-group">
<label class="axis-control-label">Injury Type:</label>
<axis-radio-button-wrapper (getSelectedItem)="getSelectedItem($event)" formControlName="injuryType"
name="injuryType" updateOn="click">
<axis-radio cid="Bumps, scratches & damage" class="_text-bold" name="insuredType"
value="Bumps, scratches & damage" heading="Bumps, scratches & damage" data-orientation="inline">
</axis-radio>
<axis-radio cid="Replacement of windows etc." name="insuredType" value="Replacement of windows etc."
heading="Replacement of windows etc." data-orientation="inline">
</axis-radio>
<axis-radio cid="Burglary in the car" name="insuredType" value="Burglary in the car"
heading="Burglary in the car" data-orientation="inline">
</axis-radio>
<axis-radio cid="Destroyed roof box, bicycle etc." name="insuredType"
value="Destroyed roof box, bicycle etc." heading="Destroyed roof box, bicycle etc."
data-orientation="inline">
</axis-radio>
<axis-radio cid="Wrong fuel" name="insuredType" value="Wrong fuel" heading="Wrong fuel"
data-orientation="inline">
</axis-radio>
<axis-radio cid="Theft of license plates" name="insuredType" value="Theft of license plates"
heading="Theft of license plates" data-orientation="inline">
</axis-radio>
</axis-radio-button-wrapper>
</div>
<div class="axis-form-group" *ngIf="damageDetailsTwoForm['controls'].injuryTypeGp.controls.injuryType.value.value === 'Burglary in the car' || damageDetailsTwoForm['controls'].injuryTypeGp.controls.injuryType.value.value === 'Theft of license plates'">
<label class="axis-control-label" for="scAlarm">Damge reported to the police:</label>
<axis-checkbox-wrapper formControlName="damageReportedToPolice">
<axis-checkbox cid="Yes" heading="Yes" name="Yes" value="Yes"></axis-checkbox>
</axis-checkbox-wrapper>
</div>
It's failing on the following line:
*ngIf="damageDetailsTwoForm['controls'].injuryTypeGp.controls.injuryType.value.value === 'Burglary in the car'
|| damageDetailsTwoForm['controls'].injuryTypeGp.controls.injuryType.value.value === 'Theft of license plates'"
Here is my ngOnInit():
ngOnInit() {
this.damageDetailsTwoForm = this.fb.group({
injuryTypeGp: this.fb.group({
injuryType: new FormControl('', Validators.required),
damageReportedToPolice: new FormControl(''),
itemOwner: new FormControl(''),
objectDescription: new FormControl(''),
}),
damageReported: new FormControl('', Validators.required),
selfRiskAmount: new FormControl('', Validators.required),
otherCompanyName: new FormControl('', Validators.required),
policyNo: new FormControl('', Validators.required),
});
}
And here is my getSelectedItem():
getSelectedItem(data) {
console.log(data);
if (data.value === 'Burglary in the car' || data.value === 'Theft of license plates') {
// tslint:disable-next-line: max-line-length
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].damageReportedToPolice.setValidators(Validators.required);
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].itemOwner.reset();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].itemOwner.clearValidators();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].objectDescription.reset();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].objectDescription.clearValidators();
} else if (data.value === 'Destroyed roof box, bicycle etc.') {
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].itemOwner.setValidators(Validators.required);
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].objectDescription.setValidators(Validators.required);
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].damageReportedToPolice.reset();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].damageReportedToPolice.clearValidators();
} else {
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].damageReportedToPolice.reset();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].damageReportedToPolice.clearValidators();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].itemOwner.reset();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].itemOwner.clearValidators();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].objectDescription.reset();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].objectDescription.clearValidators();
}
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].damageReportedToPolice.updateValueAndValidity();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].itemOwner.updateValueAndValidity();
this.damageDetailsTwoForm['controls'].injuryTypeGp['controls'].objectDescription.updateValueAndValidity();
}
Can someone please tell me why this error is occurring & how to resolve it? Thanks a lot in advance!