在angular 8模型中在数组内部创建数组时出现问题



我正在尝试创建一个名为"reportVersion";

它包含一个数组(ReportCategory(,该数组包含对象集合和两个变量,一个是数字,另一个是字符串。

我创建这个模型只是为了传递给API,作为一个参数体。

由于我还没有在提交表单中创建模型,所以需要以相同的格式将其作为参数传递给API。我需要从表单Model中获取值,并将其推送到reportVersion。此外,我将把reportVersion作为参数传递给API以调用API。

目前,我在构建reportVersion时遇到问题。我已经把下面的代码堆叠起来了。

我不明白为什么console.log(模型(没有打印到控制台。请帮忙。

this.reportCategory.push({ categoryName: 'demo1', weightage: 10 });
this.reportCategory.push({ categoryName: 'demo2', weightage: 20 });
this.reportCategory.push({ categoryName: 'demo3', weightage: 30 });
this.reportCategory.forEach((element) => {
this.reportVersion['categories'].push({
categoryName: element.categoryName,
weightage: element.weightage,
});
});
console.log('this is final output');
console.log(this.reportCategory);

https://stackblitz.com/edit/angular-ivy-vu99yk?file=src/app/hello.component.ts

在使用方法(如push(之前,您需要初始化reportCategory

this.reportCategory = []; // Added this line
this.reportCategory.push({ categoryName: 'demo1', weightage: 10 });
this.reportCategory.push({ categoryName: 'demo2', weightage: 20 });
this.reportCategory.push({ categoryName: 'demo3', weightage: 30 });

它在我身边的StackBlitz中起了作用:(

//Dylan

最新更新