Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS scope doesn't update until do something in ui, like click a object with a function

I am trying to add class to my sidebar when the viewport is less than or equal to 641px, and I have to watch the width of the window for that case

    $scope.$watch(function(){
       return $window.innerWidth;
    }, function(value) {
        if (value <= 641) {
            logger.info('!!!less than 641');
            vm.singleColumn = true;
        };
    });

It logs on first load, but when I resize, I have to do some clicks before it triggers again. E.g. I resized then I clicked on an item with ng-click behavior then that is the only time, it logs again.

I've read some of the questions, and it is likely due to $digest and $apply?

Can someone give a light to my confusion.

like image 229
Joey Hipolito Avatar asked Jun 09 '15 06:06

Joey Hipolito


2 Answers

You need to trigger the digest cycle, or the view does not realize that the value was updated.

$scope.$watch(function(){
   return $window.innerWidth;
}, function(value) {
    if (value <= 641) {
        logger.info('!!!less than 641');
        vm.singleColumn = true;
        $scope.$apply();
    };
});
like image 158
dz210 Avatar answered Sep 18 '22 09:09

dz210


According to me you can try directive and check the window resize using jquery and update your variable accordingly. I got an example here please check

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

function AppController($scope) {
    /* Logic goes here */
}

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();
        });
    }
})
div {
    border:1px solid red;
}
<div ng-app="miniapp" ng-controller="AppController" ng-style="style()" resize>window.height: {{windowHeight}}
    <br />window.width: {{windowWidth}}
    <br />
</div>

fiddle

please let me know if this works

like image 40
user1162084 Avatar answered Sep 17 '22 09:09

user1162084