为窗口大小调整创建指令

时间:2015-12-29 08:03:06

标签: angularjs highcharts window-resize

我有四个大多数方形图表,我需要在窗口调整大小时调整大小。我在顶部的角度控制器内部创建了一个指令。这似乎不起作用,因为指令似乎在页面加载后立即执行,并且在调整大小时似乎没有发生任何事情。我在这里缺少什么?

targetApp.directive('rsz', function ($window) {
    return function (scope, element) {
        var w = angular.element($window), 
            iw = w.innerWidth,
            ih = w.innerHeight;

        //resizing relative to the this parent container 
        scatterParentDimensions = getoffsetDimensions('#scatter-container'),
        expectedWidth, expectedHeight;

        console.log('rel w:', scatterParentDimensions.width, 'rel h:', scatterParentDimensions.height);

        if (iw > ih) {
            expectedWidth = scatterParentDimensions.width / 2;
            expectedHeight = (scatterParentDimensions.height < iw)
                ? scatterParentDimensions.height / 2
                : scatterParentDimensions.height / 4;
        }
        else {
            expectedWidth = iw / 2;
            expectedHeight = ih - 50;
        }

        // this is returning NaN as soon as the page is loaded.
        console.log('set w:', expectedWidth, 'set h:', expectedHeight);

        var selector = "#" + element.id;
        if ($(selector).highcharts()) {
            chart = $(selector).highcharts();
            chart.setSize(expectedWidth, expectedHeight, false);
        }

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

我在需要调整大小的四个图表上添加了指令rsz

//scatter.html
<div id="target-charts">
    <div id="scatter-container" class="col-sm-8">
        <div class="row">
            <div class="col-sm-6">
                <div class="scatter-chart">
                    <div class="chart-body" rsz id="scatterA"></div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="scatter-chart">
                    <div class="chart-body" rsz id="scatterB"></div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="scatter-chart">
                    <div class="chart-body" rsz id="scatterC"></div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="scatter-chart">
                    <div class="chart-body" rsz id="scatterD"></div>
                </div>
            </div>
        </div>
    </div>
</div>

getOffsetDimensions的定义

function getoffsetDimensions(selector) {
    var el = document.querySelector(selector);
    return {
        width: el.offsetWidth,
        height: el.offsetHeight
    }
}

2 个答案:

答案 0 :(得分:2)

在你的指令中,你听了调整大小事件但没有做任何事情(范围。$ apply基本上没有做任何事情)。

你可以在事件处理程序中实际调整大小,例如

myapp.directive('resize1', function($window) {
return function(scope, element, attr) {
    var w = angular.element($window);
    w.on('resize', function() {
        var hh = document.getElementById('header').offsetHeight;
        var fh = document.getElementById('footer').offsetHeight;
        console.log('hh & fh', hh, fh);                    

        var tp = hh + 2;
        var bt = fh + 2;
        console.log('tp & bt', tp, bt);                

        var changes = {
            bottom: bt + 'px',
            top: tp + 'px',
        }
        element.css(changes);
        scope.$apply();
    });
};
});

或观看尺寸变化,例如

app.directive('resize', function ($window) {
return function (scope, element) {
    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;

        scope.style = function () {
            return {
                'height': (newValue.h - 100) + 'px',
                    'width': (newValue.w - 100) + 'px'
            };
        };

    }, true);

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

答案 1 :(得分:0)

最终这就是我的工作方式。它仍然不是完美的解决方案,并且根本不是通用的,但我在我想要的初始阶段工作。

targetApp.directive('resize', function ($window) {
    return function (scope, element, attr) {
        var w = angular.element($window);
        scope.$watch(function () {
            return {
                'h': window.innerHeight,
                'w': window.innerWidth
            };
        }, function (newValue, oldValue) {
            var ew, eh, scatterParentDimensions = getoffsetDimensions('#scatter-container');

            if (newValue.w > newValue.h) {
                ew = scatterParentDimensions.width / 2;
                eh = (scatterParentDimensions.height < newValue.w)
                ? scatterParentDimensions.height / 2
                : scatterParentDimensions.height / 4;
            }
            else {
                ew = newValue.w / 2;
                eh = newValue.h - 50;
            }

            //resize highcharts
            var selector = '#' + element[0].id;
            var chart = angular.element(selector).highcharts();
            chart.setSize(ew, eh, false);
        }, true);

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