【问题标题】:Angular js set Openlayers map window element dimensions dynamicallyAngular js动态设置Openlayers地图窗口元素尺寸
【发布时间】:2016-10-26 15:57:06
【问题描述】:

我在一个指令的元素中有一个 Openlayers 3 映射,当用户调整浏览器大小时该指令需要正确调整大小。

wmm.directive('tchOlMap', ['$window', function ($window) {
var hgt = ($window.innerHeight - 102);
var wdth = ($window.innerWidth - 400);

var MAP_DOM_ELEMENT_ID = 'tchMap';

return {

    //restrict: 'E',
    scope: false,
    //replace: true,

    template: '<div id="' + MAP_DOM_ELEMENT_ID + '" class="full-height" style="height: '+hgt+'px; width: '+wdth+'px; padding: 0px; margin: 0px; border:2px solid;  border-color: #76AF75"></div>',

    link: function postLink(scope, element, attrs) {
     scope.isCollapsed = false;  

      var w = angular.element($window);
        scope.getWindowDimensions = function () {
        return { 'h': w.height(), 'w': w.width() };
        };

    scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {

        scope.windowHeight = newValue.h;
        scope.windowWidth = newValue.w;
        console.log(newValue.w+' '+newValue.h);
        hgt = newValue.h;
        wdth = newValue.w;

        scope.style = function () {

            return { 
                'height': (newValue.h - 102) + 'px',
                'width': (newValue.w - 400) + 'px' 
            };
        };

    }, true);

    w.bind('resize', function () {
        scope.$apply();
    });

我可以在控制台中看到它正在返回更新后的尺寸,但这些值并未应用到元素我被困在如何将尺寸应用到元素上 - 有人可以帮忙吗?

【问题讨论】:

    标签: angularjs openlayers-3


    【解决方案1】:

    感谢@pedromarc,这是最终的工作代码

    wmm.directive('tchOlMap', ['$window', function ($window) {
    
    var MAP_DOM_ELEMENT_ID = 'tchMap';
    
    return {
        //restrict: 'E',
        scope: false,
        //replace: true,
    
        template: '<div id="' + MAP_DOM_ELEMENT_ID +'" class="full-height" ng-style="style"></div>',
    
        link: function postLink(scope, element, attrs) {
          scope.style = {};
    
          scope.isCollapsed = false;  
    
          var w = angular.element($window);
            scope.getWindowDimensions = function () {
            return { 'h': w.height(), 'w': w.width() };
            };
    
           scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
    
            scope.style = { 
                    'height': (newValue.h - 102) + 'px',
                    'width': (newValue.w - 400) + 'px',
                    'padding' : 0 + 'px',
                    'margin' : 0 + 'px',
                    'border': 2 + 'px solid',
                    'border-color' : '#76AF75'
                };
    
        }, true);    
        w.bind('resize', function () {
           scope.$apply();
          });
    

    【讨论】:

      【解决方案2】:

      您正在模板中分配一个静态值,您应该更改您的模板以使用您的范围内可用的值。

      template: '<div id="' + MAP_DOM_ELEMENT_ID + '" class="full-height" ng-style="style"></div>'
      

      然后只需在链接函数中定义和更新样式,存储 scope.style 属性中所需的所有样式。

      【讨论】:

      • 感谢您的回答。我已经尝试过了,但现在我遇到了这个问题stackoverflow.com/questions/31549494/…
      • scope.style 应该是一个包含您需要的样式的对象,在您的控制器中,因为它在问题中,scope.style 存储一个函数(可能是错误),只需将您的控制器更新为我认为使用 css 定义存储对象并且应该可以工作。
      • 谢谢@pedromarc。我在链接函数的开头声明了样式对象,然后在 $watch 函数中将样式传递给它。
      猜你喜欢
      • 1970-01-01
      • 2014-11-05
      • 2014-10-30
      • 2010-10-26
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 2018-05-27
      相关资源
      最近更新 更多