Add Google Places Autocomplete
Let's use our Google API key to autocomplete the location field of the event form.
We'll cover the following...
Below is the updated code so far:
your_API_key
Not Specified...
<!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
input
element, 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.
Press + to interact
<!--src/app/event/event-create/event-create.component.html--><div class="form-group"><label>Location</label><input class="form-control"#cityautocorrect="off"formControlName="location"></div>
Adding autocomplete functionality
From here, update our imports from @angular/core
, adding NgZone
, ElementRef
, and ViewChild
.
Press + to interact
// src/app/event/event-create/event-create.component.tsimport { Component, OnInit, NgZone, ElementRef, ViewChild } from '@angular/core';
ViewChild
allows us to get an element from our view so that we have a reference to that element within our component.- Because
ViewChild
is a reference to an