I am struggling to add an object to the subCategory array within the ITemplate interface as it is resulting in the error
TypeError: Cannot read properties of undefined (reading 'subCategory')
.
Does anyone know how I can successfully push an object to the subCategory array?
Any suggestions on how to achieve this and what might be causing the error?
Thank you for your help.
#ts code
import { Component, OnInit } from '@angular/core'
interface ITemplate {
id: number;
entitlement: string;
required: boolean;
isHeaderCategory: boolean;
subCategory: ITemplate2[]
}
interface ITemplate2 {
id: number;
sub_name: string;
}
const mockData: ITemplate[] = []
@Component({
selector: 'app-template',
templateUrl: './template.component.html',
styleUrls: ['./template.component.css'],
})
export class TemplateComponent implements OnInit {
constructor() {}
ngOnInit(): void {
console.log('thiss' , this.templates)
}
add() {
this.templates[0].subCategory.push({
id: 0,
sub_name: 'test'
})
}
templates: ITemplate[] = mockData
}