【问题标题】:Google autocompleter place doesn't work in the Child Component in Angular 2谷歌自动完成器位置在 Angular 2 的子组件中不起作用
【发布时间】:2016-12-17 02:38:45
【问题描述】:

我对@9​​87654321@ 使用了googleplace 指令。 当我在 AppComponent 中使用此指令时它可以工作,如链接所示,但当我在子组件中使用它时它不起作用。

app.routes.ts

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)
];

ma​​in.ts

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


bootstrap(AppComponent,[appRouterProviders]);

app.component.ts

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

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&lt;router-outlet&gt;&lt;/router-outlet&gt;有它的内容

dashboard.component.ts

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

输出:

更新:

发现,当有一个router-outlet标签时,效果很好 在项目中,但是当我们将路由器插座嵌套为时无法工作 上面的例子有嵌套 router-outlet

Github link here

组件的子组件的指令代码有什么问题吗? 请告诉我如何解决此问题。

【问题讨论】:

  • @dfsq 代码以屏幕截图的形式发布。你要我贴出文字代码吗?
  • 可以把代码上传到github或者plunker吗?
  • @JohnSiu :在问题中添加了链接。
  • @ShoaibChikate 请检查我的答案,看看这是否是你要找的。​​span>

标签: javascript angular google-places-api angular2-directives


【解决方案1】:

问题是https://maps.googleapis.com/maps/api/place/js/AutocompletionService.GetPredictions 需要一个 api 密钥,当您在子路由器中使用它时。

index.html

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

用您的 google API 密钥代替 API_KEY。

我无法解释子组件(不需要 api 密钥)和路由器子组件(需要 api 密钥)之间的行为差​​异。

根据 Google Map Api 文档,需要 API 密钥:

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

【讨论】:

  • 我想让这个用例让它发挥作用。我不希望应用程序组件中的导航栏。能否请您也告诉我原因?。
  • @ShoaibChikate 我想我对预期的布局很困惑。你的意思是你不想在 app 组件中使用路由器,而只是使用 base.component 作为 app.component 的普通子组件?
  • 感谢您的所有努力。但我不是在应用程序组件中编写 routerLinks 。您能否从问题中提到的 github 链接检查最新提交的代码,并让我知道为什么在该路由器导航结构中不起作用。
  • @ShoaibChikate 已修复。但无法解释根本原因。
  • Sui:还是不行,还加了API密钥。请检查更新的 Github 代码。
猜你喜欢
  • 1970-01-01
  • 2016-11-11
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多