× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

AngularJS - ng-show with scope variable not working

Utkarsh Upadhyay
Apr 16, 2015
<p>You are <a href="https://docs.angularjs.org/api/ng/type/%24rootScope.Scope#%24apply" rel="nofollow">missing a <code>$scope.$apply()</code></a>:</p> <pre><code>$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(); // &lt;-- break; default: $scope.isLandscape = false; $scope.$apply(); // &lt;-- break; } }, false); </code></pre> <p>Once you change the value, Angular doesn't have a way of knowing that it should check whether the value has changed or not. <code>$scope.$apply</code> will trigger a digest loop which will make Angular notice that <code>$scope.isLandscape</code> has changed.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/28458460/AngularJS%20-%20ng-show%20with%20scope%20variable%20not%20working/28458505">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus