【发布时间】:2016-12-17 02:38:45
【问题描述】:
我对@987654321@ 使用了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)
];
import {bootstrap} from '@angular/platform-browser-dynamic';
import {AppComponent} from './app.component';
import {appRouterProviders} from './app.routes';
bootstrap(AppComponent,[appRouterProviders]);
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 {
}
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有<router-outlet></router-outlet>有它的内容
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() {
}
}
更新:
发现,当有一个router-outlet标签时,效果很好 在项目中,但是当我们将路由器插座嵌套为时无法工作 上面的例子有嵌套 router-outlet
组件的子组件的指令代码有什么问题吗? 请告诉我如何解决此问题。
【问题讨论】:
-
@dfsq 代码以屏幕截图的形式发布。你要我贴出文字代码吗?
-
可以把代码上传到github或者plunker吗?
-
@JohnSiu :在问题中添加了链接。
-
@ShoaibChikate 请检查我的答案,看看这是否是你要找的。span>
标签: javascript angular google-places-api angular2-directives