【问题标题】:Angular 2/4 Bing Map pushpin custom template function callAngular 2/4 Bing Map 图钉自定义模板函数调用
【发布时间】:2017-08-28 09:55:35
【问题描述】:

我无法将我的图钉自定义模板放入角度组件中。

我的组件:

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”变量,我需要获得适当的角度范围?

【问题讨论】:

  • 您需要自定义信息框吗?您只需将 HTML 添加到信息框的描述中,它就会呈现。

标签: javascript angular bing-maps


【解决方案1】:

如果您的意思是要从父组件访问 closeCustomInfoBox() 函数和 infoboxClick 变量。

检查这个 https://angular.io/guide/component-interaction#parent-interacts-with-child-via-local-variable

【讨论】:

  • 我用视图表示更新了我的问题。您能否在答案中提供更多详细信息。我没有父子组件,一切都是一个组件。我不知道 BingMap 如何在内部表示这个模板以及如何在我的示例中适应它。
  • 顺便检查一下stackoverflow.com/questions/37550278/…这是你想要的吗?
  • 谢谢。我已经检查过了:)。一般是关于如何添加地图,没有示例如何使用自定义图钉模板。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
相关资源
最近更新 更多