简体   繁体   中英

accessing MAT_DIALOG_DATA as value instead of reference in angular

When I am trying to access MAT_DIALOG_DATA variable its coming as ref. Ex:

@Inject(MAT_DIALOG_DATA) private data: any;

 ngOnInit() {
    this.temp = this.data;
}

update() {
this.temp= "abcd"; that. 
}

This is updating this.data . I want to update only this.temp .

How can I do that?

Try to assign this.temp to a new object

this.temp = {...this.data};

If you want shallow copy, use this.temp= Object.assign({}, this.data)

For "deep" copy, use this.temp= JSON.parse(JSON.stringify(this.data))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM