Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning Angular - Fourth Edition

You're reading from  Learning Angular - Fourth Edition

Product type Book
Published in Feb 2023
Publisher Packt
ISBN-13 9781803240602
Pages 446 pages
Edition 4th Edition
Languages
Authors (2):
Aristeidis Bampakos Aristeidis Bampakos
Profile icon Aristeidis Bampakos
Pablo Deeleman Pablo Deeleman
Profile icon Pablo Deeleman
View More author details

Table of Contents (17) Chapters

Preface 1. Building Your First Angular Application 2. Introduction to TypeScript 3. Organizing Application into Modules 4. Enabling User Experience with Components 5. Enrich Applications Using Pipes and Directives 6. Managing Complex Tasks with Services 7. Being Reactive Using Observables and RxJS 8. Communicating with Data Services over HTTP 9. Navigating through Application with Routing 10. Collecting User Data with Forms 11. Introduction to Angular Material 12. Unit Test an Angular Application 13. Bringing an Application to Production 14. Handling Errors and Application Debugging 15. Other Books You May Enjoy
16. Index

Manipulating form data

The FormGroup class contains two methods that we can use to change the values of a form programmatically:

  • setValue: Replaces values in all the controls of the form
  • patchValue: Updates values in specific controls of the form

The setValue method accepts an object as a parameter that contains key-value pairs for all form controls. If we want to fill in the details of a product in the product create component programmatically, we should use the following snippet:

this.productForm.setValue({
  name: 'New product',
  price: 150
});

In the preceding snippet, each key of the object passed in the setValue method must match the name of each control in the form. If we omit one, Angular will throw an error.

If, on the contrary, we want to fill in some of the details of a product, we can use the patchValue method:

this.productForm.patchValue({
  price...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}