Google autocomplete place not working in child component in Angular 2

I used the googleplace directive for Google places autocomplete . It works when I use this directive in the AppComponent, as shown in the link, but does not work when I used it in child components.

app.routes.ts

enter image description here

import { provideRouter, RouterConfig } from '@angular/router'; import { BaseComponent } from './components/base/base.component'; import { DashboardComponent } from './components/dashboard/dashboard.component'; const routes: RouterConfig= [ {path:"",redirectTo:"/admin",pathMatch:'full'}, {path:"admin",component:BaseComponent, children:[ { path: '', component: BaseComponent}, { path: 'dashboard', component: DashboardComponent}, ] } ]; export const appRouterProviders = [ provideRouter(routes) ]; 

main.ts enter image description here

 import {bootstrap} from '@angular/platform-browser-dynamic'; import {AppComponent} from './app.component'; import {appRouterProviders} from './app.routes'; bootstrap(AppComponent,[appRouterProviders]); 

app.component.ts enter image description here

 import {Component} from '@angular/core'; import {ROUTER_DIRECTIVES} from '@angular/router'; @Component({ selector : 'my-app', template: ` <router-outlet></router-outlet> ` , directives:[ROUTER_DIRECTIVES] }) export class AppComponent { } 

base.component.ts enter image description here

 import {Component,OnInit} from '@angular/core'; import { provideRouter, RouterConfig,ROUTER_DIRECTIVES,Router } from '@angular/router'; @Component({ selector: 'app-base', templateUrl:"../app/components/base/base.html", directives:[ROUTER_DIRECTIVES], precompile:[] }) export class BaseComponent implements OnInit{ constructor(private _router:Router){} ngOnInit():any{ this._router.navigate(["admin/dashboard"]); } } 

base.html has <router-outlet></router-outlet> has its content

dashboard.component.ts enter image description here

 import {Component,OnInit} from '@angular/core'; import { provideRouter, RouterConfig,ROUTER_DIRECTIVES,Router } from '@angular/router'; import {GoogleplaceDirective} from './../../../directives/googleplace.directive'; @Component({ selector: 'dashboard', template:` <input type="text" [(ngModel)] = "address" (setAddress) = "getAddress($event)" googleplace/> `, directives:[ROUTER_DIRECTIVES,GoogleplaceDirective] }) export class DashboardComponent implements OnInit{ constructor(private _router:Router){} ngOnInit():any{ // this._router.navigate(["dashboard/business"]); } public address : Object; getAddress(place:Object) { this.address = place['formatted_address']; var location = place['geometry']['location']; var lat = location.lat(); var lng = location.lng(); console.log("Address Object", place); } } 

googleplace.directive

 import {Directive, ElementRef, EventEmitter, Output} from '@angular/core'; import {NgModel} from '@angular/common'; declare var google:any; @Directive({ selector: '[googleplace]', providers: [NgModel], host: { '(input)' : 'onInputChange()' } }) export class GoogleplaceDirective { @Output() setAddress: EventEmitter<any> = new EventEmitter(); modelValue:any; autocomplete:any; private _el:HTMLElement; constructor(el: ElementRef,private model:NgModel) { this._el = el.nativeElement; this.modelValue = this.model; var input = this._el; this.autocomplete = new google.maps.places.Autocomplete(input, {}); google.maps.event.addListener(this.autocomplete, 'place_changed', ()=> { var place = this.autocomplete.getPlace(); this.invokeEvent(place); }); } invokeEvent(place:Object) { this.setAddress.emit(place); } onInputChange() { } } 

index.html enter image description here

Output: enter image description here

Update:

It was found that it works fine when there is one router-exit tag in the project, but does not work when we have a nested router-exit, as the example above has a nested router-exit

github link here

Is there a problem with directory code with child components of a component? Please let me know how I can solve this problem.

+6
source share
1 answer

The problem https://maps.googleapis.com/maps/api/place/js/AutocompletionService.GetPredictions requires an api key when you use it inside a child router.

index.html

 <script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&sensor=false"></script> 

Put the Google API key instead of API_KEY.

I can’t explain the difference in behavior between the child component (api key is not needed) and the child router (api key is required).

According to the Google Map Api documentation, an API key is required:

https://developers.google.com/maps/documentation/javascript/places-autocomplete

+2
source

All Articles