2

I am trying to build a login page and a functionality page using angularjs SPA.

I have following controller:
 - LoginController 
 - PredictionController

And following single page:
 - Home.html : Binded to LoginController
 - trend.html : Binded to PredictionController
 - index.html : Has no controller

I have navigation panel in index.html and I want to modify(add or remove) number of tabs based on login. I don't want to write navigation panel logic in each page as it is reusable. I am unable to figure out way to do this using additional controller because I am using ng-route which I guess won't allow me to use multiple controller for same page.

Here is my html code snippet for index.html:

<body>
        <nav class="navbar navbar-inverse navbar-fixed-top">
            <div class="container-fluid">
                <div class="navbar-header">
                  <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                  </button>
                  <a class="navbar-brand" href="#/">MyProject</a>

                </div>
                <div id="navbar" class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="#/">Home</a></li>
                        <li><a href="#Trend">Trend</a></li>
                    </ul>
                </div><!--/.nav-collapse -->
            </div>
        </nav>

    <div ng-app="chartApp">
        <div ng-view></div>                         
    </div>

</body>

Here is the code for controller:

var app = angular.module('chartApp', ['ngRoute']);

    app.factory('UserVerified', function() {
        return

 {bool: false}; 
        });


    app.config(function ($routeProvider) {
        $routeProvider
        .when('/',{
            templateUrl: 'pages/home.html',
            controller: 'LoginController'
        })
        .when('/Trend', {
            templateUrl: 'pages/trend.html',
            controller: 'PredictionController'  
        })  

    });

    app.controller('LoginController', ['$scope', '$http', 'UserVerified', function($scope, $http, UserVerified){
        $scope.hasPermission = UserVerified.bool;

    $scope.getAuthentication = function(){
        console.log($scope.userId, $scope.pwd)
        $http.get('/getAuth', {
                params: { user_id: $scope.userId, pwd: $scope.pwd }
        }).success(function (response){
            console.log(response);
            UserVerified.bool = response;
            $scope.hasPermission = UserVerified.bool;
        }); 
    }

}]);

I'm not sure if the information is enough please edit or let me know if i'm missing some information.

1 Answer 1

1

I believe you're going to have to move your ng-app directive up to the body, and create a NavigationController which can require your UserVerified factory and help maintain the state of the navigation.

You could add the ng-controller directive to your navbar explicitly, and your router should work the same.

The HTML might look something like this.

<body ng-app="chartApp">

    <nav ng-controller="NavigationController" class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <!-- Only display if $scope.loggedIn -->
                <a ng-show="loggedIn" class="navbar-brand" href="#/">MyProject</a>
            </div>

            <div id="navbar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#/">Home</a></li>
                    <li><a href="#Trend">Trend</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </nav>

    <div ng-view></div>
</body>

NavigationController

app.controller('NavigationController', function($scope, UserVerified) {
    $scope.$watch(function() {
        return UserVerified.bool;
    }, function(state, oldState) {
        $scope.loggedIn = state;
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

I thought of that but in that case how would the routing of pages and controller work? I will need a controller for login when login.html is loaded so in that case how will ngroute understand where to point.
I updated my answer with an example. The router should work exactly the same, it will load up the controller with on each route automatically.
Its working as per my requirement but when i log in, it doesn't makes them visible because '$scope.loggedIn' is not getting updated.
Updated code sample. You might need to add a $watcher to keep the controller in sync with the service. See here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.