I created a directive that adds the Angular Material flex
attribute to the leaflet
directive at compile. This makes the container width 100%. Then it gets a promise from Leaflet which is called when the map object is finished rendering. The method map::invalidateSize()
is called which forces Leaflet to check the size which is set correctly at this point of the parent container.
(function() {
angular.module('live')
.directive('ttLeafletFlexFit', leafletFlexFit);
leafletFlexFit.$inject = ['$timeout', 'leafletData'];
function leafletFlexFit($timeout, leafletData) {
var directive = {
restrict: 'A',
compile: compile
};
return directive;
function compile(tElem, attrs) {
tElem.attr('flex', '');
// A hack to get Leaflet to fill the flex container.
// @link https://github.com/tombatossals/angular-leaflet-directive/issues/950
leafletData.getMap().then(function (map) {
$timeout(function() {map.invalidateSize()});
});
}
}
})();
Then add an attribute on the leaflet directive element.
<leaflet tt-leaflet-flex-fit></leaflet>