We have a signal value with an object type and want to update one or some of that object's values.
In this case, we can use the Angular Signals update function and JavaScript spread syntax:
this.user.update((user) => ({ ...user, fistName: "First name updated!" }));
// or
this.user.update((user) => ({ ...user, fistName: "First name updated!", lastName: "Last name updated!" }));
// or
this.user.update((user) => ({ ...user, ...this.curUser }));
A complete example is here 👉 https://stackblitz.com/edit/stackblitz-starters-dprcl3?file=src%2Fmain.ts
I hope you found it helpful. Thanks for reading. 🙏
Let's get connected! You can find me on:
- Medium: https://medium.com/@nhannguyendevjs/
- Dev: https://dev.to/nhannguyendevjs/
- Hashnode: https://nhannguyen.hashnode.dev/
- Linkedin: https://www.linkedin.com/in/nhannguyendevjs/
- X (formerly Twitter): https://twitter.com/nhannguyendevjs/
- Buy Me a Coffee: https://www.buymeacoffee.com/nhannguyendevjs
Top comments (2)
Thank you
You are welcome!