Angularjs ng-show issue with ng-route

300 views Asked by At

I'm newbie to angular world. I'm trying to create a login page.When the user login, i want to show some contents in the navbar.

Currently i'm using ng-show and ng-route. When i'm not using ng-route, ng-show works fine but when i use ng-route, ng-show is not working .I don't want to use angular-ui-router. What i'm doing wrong. Can anyone help me

Angular Config

app.config(['$routeProvider', function($routeProvider) {
            $routeProvider
            .when('/', {
               templateUrl: 'login.html',
               controller: 'ctrl'
            })
            .when('/logged', {
               templateUrl: 'logged.html',
               controller: 'ctrl'  
            })
            otherwise({
               redirectTo: '/'
            });
         }]);
app.controller("ctrl",['$scope','$http','$location',function($scope,$http,$location){
            $scope.myvalue2=false;
         $scope.login = function()
         {
          //here i making the $http.post to login on success im changing $scope.myvalue2=true;
         }
         }]);

HTML

<nav class="navbar-default navbar-dark bg-primary">
   <div class="navbar">
   <div class="container-fluid navi">
     <div class="navbar-header" style="padding-bottom:10px">
                           <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                             <span class="sr-only">Toggle navigation</span>
                             <span class="icon-bar"></span>
                             <span class="icon-bar"></span>
                             <span class="icon-bar"></span>
                           </button>
                    <ul class="navbar-brand " style="list-style:none;padding-top:10px;"><li>name</li></ul>
        </div>
    <div ng-show="myvalue2" class="ng-cloak">
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1" style="padding-top:10px">
       <ul class="nav navbar-nav">
            <li><a href="pages/enhance.jsp#section0" style="font-size:14px;">About</a></li>
           <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#" style="font-size:14px;">Settings</a></li>
        </ul>
    </div>
    </div>
    </div>
    </div>
</nav>
3

There are 3 answers

1
Rohìt Jíndal On

I don't think that ngRoute would have any impact on ng-show. It's working fine check this working demo :

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

myApp.controller('MyCtrl', function($scope) {
    $scope.myvalue2 =false;
    $scope.login = function() {
      $scope.myvalue2=true;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
    <a ng-click="login()">Login</a>
    <div ng-show="myvalue2">
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1" style="padding-top:10px">
       <ul class="nav navbar-nav">
            <li><a href="pages/enhance.jsp#section0" style="font-size:14px;">About</a></li>
           <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#" style="font-size:14px;">Settings</a></li>
        </ul>
    </div>
    </div>
</div>

5
Aravind On

This will help you,

<div ng-app="myApp" ng-controller="myCtrl">
      <button class="btn btn-success" type="text" ng-model="firstName" ng-show="display" ng-click="display=!display"> BUTTON 1</button>
      <br />
      <button class="btn btn-warning" ng-click="display=!display" ng-model="lastName" ng-show="!display"> BUTTON 2
    </button>
</div>

DEMO

3
Makoto On

You're going to want to use the digest cycle here, and do away with ng-show, since its use gets easily mistaken for the more elegant and simpler-to-understand ng-if.

In short:

  • AJAX calls will trigger a digest cycle.
  • ng-if will actively remove DOM if its condition is not met, thus drawing less DOM.
  • Changing the value on an AJAX call in an ng-if will work instead of hiding the DOM element, since it'll be eligible to be visible anyway.

The ng-cloak class is throwing me off; you probably don't need that there, since with ng-if, it won't be anywhere in the DOM until you actually need it.

What your controller might look like:

app.controller("ctrl", [
    '$scope',
    '$http',
    '$location',
    function ($scope, $http, $location) {
        $scope.myvalue2 = false;
        $scope.login = function () {
          $http.get('/path/to/your/request', function(data) {
              $scope.myvalue2 = true;
          });
        }
    }]);

What your DOM would need to change to (and yes, I prefer absolute truth to truthiness):

<div ng-if="myvalue2 === true">