0

I'm trying to build a directive that keeps track of the width of a container and changes it on window resizes. I've found a lot of directives that keep track of window resizing dynamically, but is it somehow possible to rewrite the directive to still fire every resize but instead of calculating the window width on every fire, calculate the width of a specific element? So far I've used:

app.directive('monitorWindow', ['$window', function ($window) {
    return {
        link: link,
        restrict: 'A'
    };
    function link(scope, element, attrs){
        scope.windowWidth = $window.innerWidth ;

        function onResize(){
            console.log("window width is: " + $window.innerWidth);
            // uncomment for only fire when $window.innerWidth change
            if (scope.windowWidth !== $window.innerWidth)
            {
                scope.windowWidth = $window.innerWidth;
                scope.$digest();
            }
        }

        function cleanUp() {
            angular.element($window).off('resize', onResize);
        }

        angular.element($window).on('resize', onResize);
        scope.$on('$destroy', cleanUp);
    }
}]);

Which keeps track of the window size and not of a specific div. I've found a directive that gets the element dimensions but doesn't update on resizing. Both of them next to eachother for testing: https://codepen.io/mbezema/pen/odBpPo

I use this as I'm building an svg graph inside a div that calculates points and lines based on a scope.width. If that changes the whole graph needs to change.

1 Answer 1

1

In a perfect world you should use Resize Observer. But, right now it is still not ready for production because it is very limited with browsers (Chrome only)

So you can go this way and try to get some polyfills and when all browsers will support this feature you will get rid of the polyfill or try to find something else like this standalone lib or this JQuery plugin

One note. You also can listen on window resize and each time check div's width, but is not 100% correct solution because width of the div can be changed without change the width of the window.

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

I want to recalculate points of an svg graph according to the size of the div that it is in, the div will only change on changing window sizes. A resize observer looks at a resize from a user? and not at window resizing? "One note. You also can listen on window resize and each time check div's width, but is not 100% correct solution because width of the div can be changed without change the width of the window." this is exactly what i want but i cant get this working :(
@mBo Resize observer will check sizes of element it observe so this should the best option but browser support ruins this :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.