2017-08-28 54 views
0

我有問題來適合我的圖釘自定義模板到角度組件。Angular 2/4 Bing Map圖釘自定義模板函數調用

我的組分:

import { Component, AfterViewInit } from '@angular/core'; 

@Component({ 
    selector: 'app-map', 
    templateUrl: './map.component.html', 
    styleUrls: ['./map.component.scss'] 
}) 
export class MapComponent implements, AfterViewInit { 
    @ViewChild('myMap') myMap; 

    map: any; 
    infoboxClick; 
    infoboxTemplate = `<div id="infoboxText" class="custom-infobox"> 
          <span class ="close-sighn" onclick="closeCustomInfoBox()">x</span> 
          {description} 
         </div>`; 

    constructor(private dataService: MapService) { 
    } 

    ngAfterViewInit() { 
     this.getMap(); 
    } 


    getMap() { 

     if ((window as any).Microsoft && (window as any).Microsoft.Maps) { 
      this.map = new (window as any).Microsoft.Maps.Map(document.getElementById('mapId'), { 
       credentials: '' 
      }); 

      var pushpin = new (window as any).Microsoft.Maps.Pushpin(map.getCenter(), null); 
      (window as any).Microsoft.Maps.Events.addHandler(pushpin, 'click', (args) => { 
         this.infoboxClick.setOptions({ 
          location: args.target.getLocation(), 
          htmlContent: this.infoboxTemplate.replace('{description}', 'Some test description'), 
          visible: true 
         }); 
        }); 


      map.entities.push(pushpin); 

     } else { 
      setTimeout(() => { this.getMap() }, 1000); 
     }  
    } 

    closeCustomInfoBox() { 
    this.infoboxClick.setOptions({ 
     visible: false 
    }); 
    } 

} 

我的觀點:

<div #myMap id="mapId" class="map-container"></div> 

在infoboxTemplate我有功能 'closeCustomInfoBox()',這應關閉信息框。

1)我如何從我的角度組件調用該函數?

2)我需要得到適當的角度範圍,如果我稱之爲如何可以接近我的'infoboxClick'變量?

+0

你需要一個定製的信息框?您可以簡單地將HTML添加到信息框的描述中,然後渲染。 – rbrundritt

回答

0

如果您的意思是您想訪問父組件的closeCustomInfoBox()函數和infoboxClick變量。

入住這 https://angular.io/guide/component-interaction#parent-interacts-with-child-via-local-variable

+0

我用視圖表示更新了我的問題。你能否在你的答案中提供更多細節?我沒有父子組件,一切都是一個組件。我不知道BingMap如何代表這個模板,以及如何在我的示例中使用它。 – Milos

+0

Btw檢查此https://stackoverflow.com/questions/37550278/how-to-add-bing-maps-v8-to-angular-2-0 這就是你想要的? – dwij

+0

謝謝。我已經檢查過了:)。這是關於如何添加地圖一般,沒有例子如何使用自定義圖釘模板。 – Milos