From the course: Angular: API Communication and Authentication

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Writing the onSubmit() function

Writing the onSubmit() function - Angular Tutorial

From the course: Angular: API Communication and Authentication

Start my 1-month free trial

Writing the onSubmit() function

- [Instructor] Now let's start with the onSubmit function. As specified in the template, this function will take in an NgForm object. First, we set the loading flag to true. Then we make a copy of the form values using Object.assign. We can now construct the new contact that would be added. I'm using type information here, so type script is telling me what property is missing from this object. The name will consist of both the firstName and the lastName values. We add in the address, the phone, which will consist of three formValues, and lastly, the photoURL. Since we will be sending json data to our server, we need to specify that information in our request. Create a new Headers object. And we append the Content-Type header, which will hold a value application/json. Using the headers, construct a RequestOptions object. To post a new contact to our API, make a post call on the http object. The first argument will be the URL, which is api/contacts, followed by the payload, which is our…

Contents