首页 > 解决方案 > 如何更新列表中的对象

问题描述

我对一个简单的案例有奇怪的麻烦:

我得到带有事件(干预对象)的日历。

当我单击事件时,它会打开模式。

要在打开之前获取模式信息,我使用位于“CalendarServices”服务中的“findByTicket”方法:

 findByTicket(list: Intervention[], idSearch: number) {
    return (list.find((x) => x.ticketDTO.idaiticket === idSearch));
}

这是调用此方法的 TS 文件:

openEventModal (event, action) {
    console.log('Start openEventModal');
    let intervention = this.calendarServices.findByTicket(this.confirmList, event.event.id);
    if (typeof intervention === 'undefined') {
        intervention = this.calendarServices.findByTicket(this.interventionInitList, event.event.id);
        if (typeof intervention === 'undefined') {
            console.log('nouveau ticket => ' + typeof intervention);
            intervention = {} as Intervention;
            // blabla
        } else {
            console.log('this.interventionInitList avant open: ');
            console.log(this.interventionInitList);
            console.log('intervention existante => ' + typeof intervention);
        }
    } else {
        console.log('ticket ou intervention réédité ' + typeof intervention );
    }
    const dialogRef = this.dialog.open(ModalInterventionComponent, {
        height: '50%',
        width: '50%',
        maxWidth: '100vw',
        maxHeight: '100vh',
        autoFocus: false,
        data: {
            myJarviaServices: this.myJarviaServices,
            contactsList: this.contactsList,
            prestatairesList: this.prestatairesList,
            typesList: this.typesList,
            intervention: intervention,
            action: action
        }
    });

    dialogRef.afterClosed().subscribe(result => {
        if (result !== null) {
            console.log('this.interventionInitList en sortie de modal: ');
            console.log(this.interventionInitList);
            if (action === 1) { 
                // case 1
            } else {  
                 // case 2
            }
        } else {
            if (action === 1) {
               // case 3
            }
        }
    });
}

在这种情况下: 通过使用“findByTicket”函数在“interventionInitList”列表中检索干预对象,其中包含每个干预的原始信息(有2个干预)。

当我在更新信息后关闭模式时会出现问题:“interventionInitList”项目也更新为我在模式中选择的值!为什么 ???

以下是使用原始信息打开之前的日志(console.log 说明):Intervention object is sent to modal

在此处输入图像描述

这是在模式中更新干预对象信息后的日志:

在此处输入图像描述

该项目也在更新。但我从来没有在我的代码中更新interventionInitList ...

为什么-它保留此“链接”,我该如何解决?我只想检索此列表中的原始信息,但不更新它

标签: angulartypescriptarraylistangular6

解决方案


推荐阅读