2016-11-24 3 views
1

구성 요소에서 구성 요소를 사용할 때 사례가 있습니다. @ViewChild를 통해 하위 구성 요소의 속성에 액세스하여 자식 구성 요소의 메서드에 대해 정의되지 않은 상태가됩니다.각도 2, Google지도에서 사용하는 하위 구성 요소가 컨텍스트를 잃는 이벤트

// Parent component 

declare var google: any; 
@Component({ 
    selector: 'app-map', 
    templateUrl: ` 
    <div id="map"></div> 
    <app-context-menu></app-context-menu>`, 
    styleUrls: ['./map.component.css'], 
    providers: [CitiesService], 
}) 

export class MapComponent implements AfterViewInit { 
    @ViewChild(ContextMenuComponent) contextMenu: ContextMenuComponent; 
    user: UserInterface; 
    city: any; 
    map: any; 

    constructor(
    private userStorage: UserStorage, 
    private citiesService: CitiesService, 
    private theMap: TheMap, 
) {} 

    ngAfterViewInit() { 
    this.findUserCityCoords(); 
    } 

    inittializeMap(mapOpts) { 

    let mapProps = { 
     center: new google.maps.LatLng(mapOpts.lat, mapOpts.lng), 
     zoom: mapOpts.zoom, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    let map = new google.maps.Map(document.getElementById('map'), mapProps); 
    let menu = this.contextMenu; 

    map.addListener('rightclick', (e) => { 
     console.log('map', this); 
     console.log('menu', this.contextMenu); 
     this.contextMenu.open(e); 
    }); 
    } 

    findUserCityCoords() { 
    let userCity = this.userStorage.getFromStorage().city; 

    this.citiesService.getCityConfig() 
     .subscribe(cities => { 
     cities.forEach(city => { 
      if (city.name === userCity) this.inittializeMap(city.center); 
     }); 
     }); 
    } 

'지도 이벤트 수신기'가 'menu.open (e);' 컨텍스트가 변경되고 하위 구성 요소 내부에서 하위 구성 요소의 메서드를 사용할 수 없습니다. 즉 'this.draw()'메소드를 호출합니다. 내가 제대로 자녀의 구성 요소 방법을 사용하는 구글의지도 (아마도 '바인딩'과 함께) '오른쪽 클릭하면'이벤트 리스너를 구현할 수있는 방법

// Child component 
import { Component, OnInit, ElementRef, AfterViewInit } from 

'@angular/core'; 
import { TheMap } from '../../services/mapServices/TheMap'; 
import { Marker } from '../../services/mapServices/marker.service'; 
declare var google: any; 

@Component({ 
    selector: 'app-context-menu', 
    templateUrl: './context-menu.component.html', 
    styleUrls: ['./context-menu.component.css'] 
}) 
export class ContextMenuComponent { 
    overlay; 
    el; 

    constructor(
    private theMap: TheMap, 
    private marker: Marker, 
    private elementRef: ElementRef 
) { 
    this.overlay = new google.maps.OverlayView(); 
    } 

    ngAfterViewInit() {} 

    open(e) { 
    let map = this.theMap.getMap(); 
    this.overlay.set('position', e.latLng); 
    this.overlay.setMap(map); 
    this.draw(); // <---- Here, this.draw() is not a function 
    // So I can access properties from constructor but not this.draw() and other methods of this class 
    }; 

    draw() { 
    // .... 
    } 

. `} {개방 (E)`후`, 감사

+0

찾고있는 수 있습니다 이해 –

답변

1

확실하지 내가 질문하지만 () => (화살표 기능) 당신은 내가 당신을 '제거 할 필요가 있다고 생각

map.addListener('rightclick', (e) => { 
    console.log('map', this); 
    console.log('menu', menu); 
    this.contextMenu.open(e); 
}); 
+0

OP는 이미 그 범위 밖에서'menu.'를'this.contextmenu'로 설정했기 때문에 이것이 아무 것도 변하지 않을 것이라고 생각합니다. – echonax

+1

적어도 문제가 무엇인지에 대한 더 많은 통찰력을 줄 수있는 응답을 이끌어 낼 수도 있습니다 .- 나에게이 "해결 방법"없이 콜백 내부에서'this '작업을 수행하는 방법을 찾고있는 것 같았습니다. –

+1

예, 아마도 :) – echonax

관련 문제