简体   繁体   中英

Detect change in a variable of Angular component

I got an array of objects, in similar structure to this:

arr = [ { key1: 'test', key2: 'text' } ]

This array and the object keys and values in it my be change, then possibly go back to original with user interactions on the page.

I got an unsaved changes feature to develop, where when I leave the component I wanna see if there were any changes (including deeply nested ones) in this array.

Is there a way to detect this change in angular?

I suppose you could use a service to store the original values. You could compare the current value to the original to see if there is a difference, and be able to reset it like you wanted.

when I leave the component I wanna see if there were any changes (including deeply nested ones)

The "when I leave" part suggests using the ngOnDestroy hook. Now, dependent on your use case, you can emit the current value to component's output, or feed it to a shared service (you didn't share much about your application's architecture).

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