Angular 2数组在仅编辑一个元素时为所有元素添加属性

时间:2017-10-18 10:42:27

标签: angular typescript

在标题中,我正在尝试编辑一个我拥有索引的元素,当我这样做时,所有元素都被编辑。我在list中添加了一些元素,这是我模型的属性,但是该元素被添加到所有elemenets列表中而不是仅添加到此列表中。

 unitsViewModel: Array<UnitViewModel> = [];
 mapUnitToViewModel(unit: Unit, serverId: number) {
    var index = this.checkIfUnitExist(unit.code);
    if (index) {
        //here I am adding component to componenets list(property of unitViewModelwhh)
        this.unitsViewModel[index].component.push({
            componentId: unit.id,
            serverId: serverId
        });
    }
    else {
        this.componenetArray.length = 0;
        this.componenetArray.push({
            componentId: unit.id,
            serverId: serverId
        });

        this.unitsViewModel.push(
            {
                id: ++this.idIndex,
                component: this.componenetArray,
                label: unit.code
            });
    }
}

0 个答案:

没有答案