Javascript 将Typescript对象按角度推送到对象数组的步骤

Javascript 将Typescript对象按角度推送到对象数组的步骤,javascript,arrays,angular,typescript,javascript-objects,Javascript,Arrays,Angular,Typescript,Javascript Objects,我的课程如下: export class Template { public Id: Number; public FileName: string; } 下面的代码段来自一个函数,我想首先创建一个Template类型的对象并将其推送到数组中。代码在临时Id处中断,错误为: 错误类型错误:无法设置未定义的属性“Id” 模板最初为空。模板是包含一些文件名的数组。数组声明为:templateObjects:Template[]=新数组 你没有更新模板 让temp只是声明变量,但新的初始化i

我的课程如下:

export class Template {
  public Id: Number;
  public FileName: string;
}
下面的代码段来自一个函数,我想首先创建一个Template类型的对象并将其推送到数组中。代码在临时Id处中断,错误为:

错误类型错误:无法设置未定义的属性“Id”

模板最初为空。模板是包含一些文件名的数组。数组声明为:templateObjects:Template[]=新数组


你没有更新模板

让temp只是声明变量,但新的初始化itallocate内存,然后u将能够为其分配其他属性

for (let i = 0; i < this.templates.length; i++)  {
  let temp = new Template() ;
  temp.Id = i;
  temp.FileName = this.templates[i].toString();
  this.templateObjects.push(temp);
}

嘿,哈南,非常感谢!你刚刚为我解决了一件我苦苦挣扎了几个小时的事情。
for (let i = 0; i < this.templates.length; i++)  {
  let temp = new Template() ;
  temp.Id = i;
  temp.FileName = this.templates[i].toString();
  this.templateObjects.push(temp);
}