Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - ng-show with scope variable not working

I am trying to hide/show certain things when the orientation of the phone changes.

Here is my view:

<div ng-controller="ModelCtrl">
    <p ng-show="isLandscape">Landscape - {{ isLandscape }}</p>
    <p ng-show="!isLandscape">Portrait - {{ !isLandscape }}</p>
</div>

And here is the controller snippet:

    $scope.isLandscape = false;

    // Listen for orientation changes
    window.addEventListener("orientationchange", function() {
        // Announce the new orientation number
        switch(window.orientation) 
        {  
          case -90:
          case 90:
            $scope.isLandscape = true;
            alert($scope.isLandscape);
            break; 
          default:
            $scope.isLandscape = false;
            alert($scope.isLandscape);
            break; 
        }
    }, false);

I am able to alert the correct value of isLandscape on orientation change, but it seems it does not update the view, so it is always saying its in "portrait mode - true".

like image 816
user3562751 Avatar asked Feb 11 '15 15:02

user3562751


People also ask

What is ng-show in AngularJS?

AngularJS ng-show directive is an In-Built AngularJS directive which can be used on HTML View Page to hide or show a particular section (div, input, etc.). In the HTML view, which is based on evaluation of expression defined in ng-show.

How to use the $scope object in AngularJS?

How to Use the Scope? When you make a controller in AngularJS, you pass the $scope object as an argument: When adding properties to the $scope object in the controller, the view (HTML) gets access to these properties. In the view, you do not use the prefix $scope, you just refer to a property name, like { {carname}}.

How to show or hide an HTML element in AngularJS?

The ngShow directive shows or hides the given HTML element based on the expression provided to the ngShow attribute. The element is shown or hidden by removing or adding the .ng-hide CSS class onto the element. The .ng-hide CSS class is predefined in AngularJS and sets the display style to none (using an !important flag).

How do I run JavaScript code when a value changes in AngularJS?

AngularJS binding, with ng-model and the double curly bracket syntax, does not allow you to run JavaScript code when a value changes. Thankfully, it does provide a $scope function, $watch (), for this purpose. Let's set up a simple watch to show how we can run additional code when values change. The $watch () is a function on the $scope service.


1 Answers

You are missing a $scope.$apply():

$scope.isLandscape = false;

// Listen for orientation changes
window.addEventListener("orientationchange", function() {
    // Announce the new orientation number
    switch(window.orientation) 
    {  
      case -90:
      case 90:
        $scope.isLandscape = true;
        $scope.$apply(); // <--
        break; 
      default:
        $scope.isLandscape = false;
        $scope.$apply(); // <--
        break; 
    }
}, false);

Once you change the value, Angular doesn't have a way of knowing that it should check whether the value has changed or not. $scope.$apply will trigger a digest loop which will make Angular notice that $scope.isLandscape has changed.


A better solution is to inject $window into your controller and add the orientationchange event listener to it instead. That will obviate the need for the $scope.$apply.

like image 56
musically_ut Avatar answered Oct 14 '22 05:10

musically_ut