简体   繁体   中英

Angular.js - View doesn't update when changing a scope variable in resize event

I want to change the scope variable tab to "home" every time the window size got changed. The following code does it well, but for some reason, the view doesn't get updated.

$scope.changeTab = function (tab) {
  $scope.tab = tab;
};
    
angular.element($window).bind('resize', function () {
  $scope.changeTab('home');
});
<div>{{ tab }}</div>

You should wrap your $scope.closeMoreTab(); into $scope.$apply function, as this currently changes value, but doesn't trigger digest cycle:

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

您可以尝试将变量添加到$watch ,因为每当该变量的值发生变化时, $apply将被调用,并且调用$apply将处理$digest循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM