[Angular] Using ngOnChanges lifeCycle hook to break object reference
What could be the issue, for example we have two list:
Parent component:
@Component({ selector: 'passenger-dashboard', styleUrls: ['passenger-dashboard.component.scss'], template: ` <div> <passenger-count [items]="passengers"> </passenger-count> <div *ngFor="let passenger of passengers;"> {{ passenger.fullname }} </div> <passenger-detail *ngFor="let passenger of passengers;" [detail]="passenger" (edit)="handleEdit($event)" (remove)="handleRemove($event)"> </passenger-detail> </div> ` }) export class PassengerDashboardComponent implements OnInit { passengers: Passenger[]; constructor() {} ngOnInit() { this.passengers = [{ id: 1, fullname: 'Stephen', checkedIn: true, checkInDate: 1490742000000, children: null }, { id: 2, fullname: 'Rose', checkedIn: false, checkInDate: null, children: [{ name: 'Ted', age: 12 },{ name: 'Chloe', age: 7 }] }, { id: 3, fullname: 'James', checkedIn: true, checkInDate: 1491606000000, children: null }, { id: 4, fullname: 'Louise', checkedIn: true, checkInDate: 1488412800000, children: [{ name: 'Jessica', age: 1 }] }, { id: 5, fullname: 'Tina', checkedIn: false, checkInDate: null, children: null }]; } handleEdit(event: Passenger) { this.passengers = this.passengers.map((passenger: Passenger) => { if (passenger.id === event.id) { passenger = Object.assign({}, passenger, event); } return passenger; }); } handleRemove(event: Passenger) { this.passengers = this.passengers.filter((passenger: Passenger) => { return passenger.id !== event.id; }); } }
Child component:
@Component({ selector: 'passenger-detail', styleUrls: ['passenger-detail.component.scss'], template: ` <div> <span class="status" [class.checked-in]="detail.checkedIn"></span> <div *ngIf="editing"> <input type="text" [value]="detail.fullname" (input)="onNameChange(name.value)" #name> </div> <div *ngIf="!editing"> {{ detail.fullname }} </div> <div class="date"> Check in date: {{ detail.checkInDate ? (detail.checkInDate | date: 'yMMMMd' | uppercase) : 'Not checked in' }} </div> <div class="children"> Children: {{ detail.children?.length || 0 }} </div> <button (click)="toggleEdit()"> {{ editing ? 'Done' : 'Edit' }} </button> <button (click)="onRemove()"> Remove </button> </div> ` }) export class PassengerDetailComponent implements OnInit { @Input() detail: Passenger; @Output() edit: EventEmitter<any> = new EventEmitter(); @Output() remove: EventEmitter<any> = new EventEmitter(); editing: boolean = false; constructor() {} ngOnInit() { console.log('ngOnInit'); } onNameChange(value: string) { this.detail.fullname = value; } toggleEdit() { if (this.editing) { this.edit.emit(this.detail); } this.editing = !this.editing; } onRemove() { this.remove.emit(this.detail); } }
They both display list of "passengers".
What will happens that when we change the "passenger" value in 'passenger-detail' component. It will using '(edit)' event to update parent component's 'passengers' variable.
Both changes happens in the same time.
But what we really want is, until child component click "Done" button then parent component get udpate.
So what we can do is using 'ngOnChanges' in child component to break Javascript object reference (this.detail, which marked in yellow background).
export class PassengerDetailComponent implements OnChanges, OnInit { constructor() {} ngOnChanges(changes) { if (changes.detail) { this.detail = Object.assign({}, changes.detail.currentValue); } console.log('ngOnChanges'); } onNameChange(value: string) { this.detail.fullname = value; } ... }
We use 'Object.assign' to create a new 'this.detail' object reference.
And because of this new creation, it actually breaks the reference of 'this.detail'.
And only when click "Done" button, the deatial will be sent to the parent component.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2015-01-18 [AngularJS] New in Angular 1.3 - $httpProvider.useApplyAsync