Add Google Places Autocomplete
Explore how to enhance an Angular event creation form by integrating Google Places Autocomplete. Learn to use Angular's ViewChild to reference input elements, NgZone to manage asynchronous updates, and MapsAPILoader to load the Google Places API. This lesson guides you through setting up autocomplete for city input, handling asynchronous data, and updating the form to improve user experience.
We'll cover the following...
We'll cover the following...
Below is the updated code so far:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>LetsGetLunch</title> <base href="/"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> </head> <body> <app-root></app-root> </body> </html>
Updated create event form
Update event creation form
Address the location input.
- Uncomment the HTML for location.
- On the
inputelement, add the template variable#city. We’ll reference this shortly once we implement the autocomplete for Google Places. - Add
autocorrect="off"to prevent the browser from attempting to autocorrect any text within this input, because Google will handle it for us.
Adding autocomplete functionality
From here, update our imports from @angular/core, adding NgZone, ElementRef, and ViewChild.
ViewChildallows us to get an element from our view so that we have a reference to that element within our component.- Because
ViewChildis a reference to an element within our view, we’ll set its type asElementRef. - We’ll cover what
NgZonedoes soon, when we add the Google