【发布时间】:2018-12-03 07:09:52
【问题描述】:
我有以下指令,
import { Directive, Input, OnInit } from '@angular/core';
import {GoogleMapsAPIWrapper} from '@agm/core';
declare let google: any;
@Directive({
// tslint:disable-next-line:directive-selector
selector: 'appCycleDirection'
})
export class CycleDirectionDirective implements OnInit {
@Input() org;
@Input() dst;
@Input() originPlaceId: any;
@Input() destinationPlaceId: any;
@Input() waypoints: any;
@Input() directionsDisplay: any;
@Input() estimatedTime: any;
@Input() estimatedDistance: any;
constructor(private gmapsApi: GoogleMapsAPIWrapper) { }
ngOnInit() {
console.log(' In directive ');
console.log(this.org);
this.gmapsApi.getNativeMap().then(map => {
const directionsService = new google.maps.DirectionsService;
const directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
directionsService.route({
origin: {lat: this.org.latitude, lng: this.org.longitude},
destination: {lat: this.dst.latitude, lng: this.dst.longitude},
waypoints: [],
optimizeWaypoints: true,
travelMode: 'TRANSIT'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
console.log(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
});
}
}
我在其他组件html中使用这个指令如下,
<agm-map *ngIf="Address !== undefined" [latitude]="lat" [longitude]="lng" [zoom]="zoom">
<appCycleDirection *ngIf="dst !== undefined" [org]="org" [dst]="dst"></appCycleDirection>
<agm-marker [style.height.px]="map.offsetHeight" [latitude]="lat" [longitude]="lng"></agm-marker>
</agm-map>
我已将指令添加到根模块 app.module.ts 中的声明数组和导出数组中。
即使我已经在上面的html页面中导入了组件。
但是当我运行代码时,它会抛出以下错误,
> Uncaught (in promise): Error: Template parse errors: Can't bind to
> 'org' since it isn't a known property of 'appCycleDirection'.
> ("[latitude]="lat" [longitude]="lng" [zoom]="zoom">
> <appCycleDirection *ngIf="dst !== undefined" [ERROR ->][org]="org" [dst]="dst"></appCycleDirection>
> <agm-marker [style.height.px]="map.offsetHeight" ["): ng:///LayoutModule/FindCycleComponent.html@9:50 Can't bind to 'dst'
> since it isn't a known property of 'appCycleDirection'. ("lat"
> [longitude]="lng" [zoom]="zoom">
> <appCycleDirection *ngIf="dst !== undefined" [org]="org" [ERROR ->][dst]="dst"></appCycleDirection>
> <agm-marker [style.height.px]="map.offsetHeight" [latitude]="l"): ng:///LayoutModule/FindCycleComponent.html@9:62
> 'appCycleDirection' is not a known element:
> 1. If 'appCycleDirection' is an Angular component, then verify that it is part of this module.
> 2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("> <agm-map
> *ngIf="cycleAddress !== undefined" [latitude]="lat" [longitude]="lng" [zoom]="zoom">
> [ERROR ->]<appCycleDirection *ngIf="dst !== undefined" [org]="org" [dst]="dst"></appCycleDirection>
> <agm-m"): ng:///LayoutModule/FindCycleComponent.html@9:4
请纠正我哪里出错了。自己尝试了很多时间,但我找不到任何东西。
【问题讨论】:
-
是的,但我已将其设置为未定义,但随着执行的进行,它会有所不同。这里是初始初始化,cycleAddress = undefined;方向显示=未定义;纬度 = 12.967197;液化天然气 = 77.717973;缩放 = 12; dst = 未定义; org = 未定义;
标签: angular angular-directive angular-google-maps