A newly created interface named Test in Angular 11 contains the following properties:
export interface Test {
id:number;
name:string;
}
After exporting the interface, an array named ProvinceAllStudent was created to store test data.
import { Test } from './../../../student/Models/test';
import { Component, OnInit } from '@angular/core';
import { Province } from 'src/app/student/Models/Province.model';
@Component({
selector: 'app-general-statistic',
templateUrl: './general-statistic.component.html',
styleUrls: ['./general-statistic.component.css']
})
export class GeneralStatisticComponent implements OnInit {
Provinces:any[];
ProvinceAllStudent:Test[]=[];
constructor(
) { }
ngOnInit(): void {
this.CalculateProvinceStudents()
}
CalculateProvinceStudents()
{
for(let j=0;j<5;j++)
{
this.ProvinceAllStudent[j].id=j;
this.ProvinceAllStudent[j].name='A';
}
}
}
An error occurred when running the application:
core.js:6210 ERROR TypeError: Cannot set properties of undefined (setting 'id') at GeneralStatisticComponent.CalculateProvinceStudents (general-statistic.component.ts:23)