AngularJS $ watch to change element width

I tried the internet for the last 3 days, trying to figure out how to get a directive when angular notices a change in the width of the div. I continue to see the same examples of how I should achieve this, but they do not work for me, and I do not know why.

I'm on the fourth again. All I'm trying to do is: update the css div property (which has a directive as an attribute) when the width of the div changes.

I need to know how to get angular to check if a property has changed.

This is what I still have:

.directive('stayPut', function () {
    var linker = function (scope, element, attrs) {

        scope.getElemWidth = function () {
            return element.width();
        };

        scope.$watch(scope.getElemWidth, 
            function (newWidth, oldWidth) {
                var deltaWidth = newWidth - oldWidth;

                console.log(deltaWidth);

                element.css('top', 
                    (parseInt(element.css('top')) + 
                    deltaWidth)                   +
                    'px');
        }, true);

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

    return {
        link: linker,
        restrict: 'A'
    }
});

I know the problem is here:

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

I have a vague understanding of how this works, but I do not understand why it does not work here.

+4
source share
1 answer

Try using this:

element.on('resize', function () {
    scope.$apply();
});
0
source

Source: https://habr.com/ru/post/1599708/


All Articles