angularjshttpgetng-showangularjs-ng-show

angularjs ng-show not working without http get


I have a problem. In my html file I have a following content:

<div ng-controller='nav'>
                <ul >               
                    <li ng-show="sh" >
                        <a ui-sref="problems">name</a>                        
                    </li>

            </div>

My controller is:

app.controller('nav', function($scope, $state, $http){
        $(function(){        

            $scope.sh=true;
            //$http.get('something');
        });
    });    

If

$http.get('something')

is commented, ng-show does not work. However, if I uncomment it, it starts to work. I cannot understand the reason of this. Did you have a similar problem?


Solution

  • Try this below code.

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8" />
    </head>
    <body>
        <div ng-controller='nav'>
            <ul>
                <li ng-show="sh">
                    <a ui-sref="problems">name</a>
                </li>
            </ul>
        </div>
    
        <script src="../lib/angular.js"></script>
        <script>
            var app = angular.module('app', []);
            app.controller('nav', function ($scope, $state, $http) {
                    $scope.sh = true;
                    $http.get('something')
            });
        </script>
    </body>
    </html>
    

    I dont know why you included the jquery part. It is always good to not to use jquery in AngularJS projects. You will get almost all things in AngularJS one way or the another.