AngularJs directive reports wrong height

2019-08-19 05:11发布

问题:

I have created the jsFiddle to describe my problem. http://jsfiddle.net/rzajac/MMud3/

The directive itself is here:

var myApp = angular.module('myApp', []);

myApp.directive('map', function(){
  return {
    restrict: 'E',
    scope: {},
    link: function(scope, elem, attrs)
    {
      elem
        .height(400)
        .width(600)
        .vectorMap({
          map: 'usa_en',
          backgroundColor: null,
          color: '#ffffff',
          hoverColor: '#999999',
          selectedColor: '#666666',
          enableZoom: false,
          showTooltip: true
        });

      document.getElementById('mapHeight').value = elem.height();
    }
  }
});

The directive is creating html and SVG elements inside itself. But when I'm trying to get the height of created map i get 19 instead of 400. What am I doing wrong?

回答1:

It seems that there are some default styles for the <map> tag that are affecting the height calculation.

The following css should fix the problem:

map {
    display: block;
}

Another way to fix it is to change the directive declaration style to attribute (A). Change <map> to <div map></div> and on the directive restrict: 'E' to restrict: 'A'.