如何在 Angular 中的不同 Json 数组中使用相同的模型名称 2/4

How to use same model name in different Json array in Angular 2/4

我有两个 Json 数组,如下所示。

Items: any[]; Item: any = {};

两个数组具有相同的 属性 名称 ItemName; Items 数组包含项目详细信息列表。如果我 select Items 数组中的一项并复制到 Item。当我更新 Item 的值时,它也会反映在 Items 中。如何在 Angular 2/4

中阻止此操作

您需要提取所选项目并对其进行深度克隆,然后对其进行操作,而不是对数组中的项目进行操作。你可以这样做:

Items: any[]; // Your original array
Item: any = {}; // One of the items in the previous array

selectedItem: any = Object.assign({}, Item); // use this item instead of the previous one.

希望对您有所帮助! :)