Display content in navigation menu in angular mode

My html page contains two sections. Left side I have navigation (code below) and right side is content display for the corresponding navigation menu.

When clicking on each navigation menu, a specific section should appear in my content piece.

I did it using ng-show / ng-hide by setting a flag in the js file. But since I am setting flags for each section and showing / hiding, my js code looks very strange. I know this is not the correct way to do it.

I am new to angularjs. Please help me to achieve this efficiently.

HTML code: (only part of navigation)

<div class="col-md-2">
<ul class="nav nav-pills nav-stacked">
<li class="active"><a href="#Home" data-toggle="collapse" data-target="#Login">Home</a> </li>
<div class="collapse" id="Login">
<ul class="nav nav-pills nav-stacked">
<li><a class="glyphicon glyphicon-log-in" href="#" ng-click="LoginScreen()">Login</a></li>
<li><a class="glyphicon glyphicon-user" href="#" ng-click="RegisterScreen()">Register</a></li>
</ul>
</div>
<li ><a href="#Contact" data-toggle="collapse" data-target="#Contact">About</a></li> 
<div class="collapse" id="Contact">
<ul class="nav nav-pills nav-stacked">
<li><a class="glyphicon glyphicon-save" href="#" ng-click="LoadUserDetailsScreen()">LoadUserDetails</a></li>
<li><a class="glyphicon glyphicon-phone-alt" href="#">Contact</a></li>
</ul>
</div>
</ul>
</div>

      

JS code:

var app = angular.module('myApp', []);
app.controller('mycontroller', function($scope) {

$scope.FlagHomeScreen = true;
$scope.LoadData=false;
$scope.showtable=false;
$scope.showstatus=false;
$scope.FlagLoginScreen = false;
$scope.RegisterScreenFlag= false;

$scope.menuitems =[10,20,30];

$scope.LoginScreen = function(){
$scope.FlagLoginScreen = true;
$scope.FlagHomeScreen =false;
$scope.LoadData=false;
$scope.RegisterScreenFlag=false;
};
$scope.LoadUserDetailsScreen =function(){
$scope.LoadData=true;
$scope.FlagLoginScreen = false;
$scope.FlagHomeScreen =false;
$scope.RegisterScreenFlag=false;
};

$scope.RegisterScreen=function(){
$scope.RegisterScreenFlag=true;
$scope.LoadData=false;
$scope.FlagLoginScreen = false;
$scope.FlagHomeScreen =false;
};
});

      

+3


source to share


1 answer


You can handle navigation in your application with ui-router. For your case, you need a main template that includes a navigation bar, and in each state of your application, you want to show a different view.

Here is a fiddle that shows you how you can use ui-router to handle your views. For clarity, I have only logged in and user data in the example below. You can add the rest of the pieces yourself, this will be good practice for you.

Define your states as such:

var myApp = angular.module('myApp', ['ui.router'])
    .config(['$stateProvider', function ($stateProvider) {

    $stateProvider.state('home', {
        url: "/",
        template: '<div ui-view=""/>' // templates of child states will fill components with ui-view attribute
    })
    .state('home.userDetails', {
        url: "userDetails",
        template: '<div>user details</div>',
        controller: "UserDetailsCtrl"
    })
    .state('home.login', {
        url: "login",
        //templateUrl: "path/to/login.html" // instead of giving inline templates as below, you can include your template into a html file, and use it in your state by templateUrl property
        template: '<div>user login</div>',
        controller: "LoginCtrl"
    })
}])

      



And you can navigate within your states with ui-sref instead of using ng-show and ng-click.

<div class="col-md-2">
<ul class="nav nav-pills nav-stacked">
<li class="active"><a href="#Home" data-toggle="collapse" data-target="#Login">Home</a> </li>
<div class="collapse" id="Login">
<ul class="nav nav-pills nav-stacked">
<li><a class="glyphicon glyphicon-log-in" ui-sref="home.login">Login</a></li>
<li><a class="glyphicon glyphicon-user" href="#" ng-click="RegisterScreen()">Register</a></li>
</ul>
</div>
<li ><a href="#Contact" data-toggle="collapse" data-target="#Contact">About</a></li> 
<div class="collapse" id="Contact">
<ul class="nav nav-pills nav-stacked">
<li><a class="glyphicon glyphicon-save" ui-sref="home.userDetails">LoadUserDetails</a></li>
<li><a class="glyphicon glyphicon-phone-alt" href="#">Contact</a></li>
</ul>
</div>
</ul>
</div>
<div ui-view=""/>

      

Don't forget to check the ui-router documentation: http://angular-ui.github.io/ui-router/site/#/api

JSFiddle: http://jsfiddle.net/7tzXh/48/

0


source







All Articles