First I tried the accepted answer and ran into a problem that some people had in the comments, where when loading 'prototype' it was null.
At first I solved this using try / catch with setTimeout in catch, which will try to load bing in a second. It worked, but it was a very messy decision.
In the end, I looked for people loading Google Maps in angular to see if there was a better solution. Fortunately, there are promises and uses.
The solution that worked for me was found here in this answer. Full credit for them goes to them.
First create a service to download your map ...
forklift card-service.service
import { Injectable } from '@angular/core'; const url = 'http://www.bing.com/api/maps/mapcontrol?callback=__onBingLoaded&branch=release'; @Injectable() export class BingMapsLoader { private static promise; public static load() { // First time 'load' is called? if (!BingMapsLoader.promise) { // Make promise to load BingMapsLoader.promise = new Promise( resolve => { // Set callback for when bing maps is loaded. window['__onBingLoaded'] = (ev) => { resolve('Bing Maps API loaded'); }; const node = document.createElement('script'); node.src = url; node.type = 'text/javascript'; node.async = true; node.defer = true; document.getElementsByTagName('head')[0].appendChild(node); }); } // Always return promise. When 'load' is called many times, the promise is already resolved. return BingMapsLoader.promise; } }
In the parent component, the component containing the bing map element has this code ...
import { BingMapsLoader } from './services/map-loader-service/map-loader-service.service'; export class BingMapParentComponent { mapReady = false; constructor() { BingMapsLoader.load() .then(res => { console.log('BingMapsLoader.load.then', res); this.mapReady = true; }); } }
In addition, the template of the parent component has this code, which will prevent the initialization of the component of binge cards until it is ready.
<app-bing-map *ngIf='mapReady'></app-bing-map>
Now, at bing-map.component , we want to wait until the component is in the DOM before loading the map.
ngOnInit() { if (typeof Microsoft !== 'undefined') { console.log('BingMapComponent.ngOnInit'); this.loadMap(); } }
And finally, you upload the bing map to bing-map.component
loadMap() { this.map = new Microsoft.Maps.Map(document.getElementById('mapId'), { credentials: 'Your Bing Maps Key Here', }); }