-1

Am new to the angular js. Am implementing a nested ui view but the problem is when checking the current state of a page it returns many objects such that i cant use $state.current to set the ng-show

I would like the navbar shown and hidden in some states.

I have tried

The main index.html page
<html>......
<body ng-app="myapp">
<nav ng-controller="headerCtrl" ng-show="shownav()" >
  //here is the navbar code..The shownav is defined on the headerCtrl

</nav>

<div ui-view>   </div>

//Angular js, controllers and services links


</body>
</html>

The app.js code

var app = angular.module('myApp', ['ui.router']);
app.controller('headerCtrl',function($scope, $state) {
    $scope.shownavbar = function(){
        var state = $state.current;
       if(state ==='login' ){
       return false;
       }else{
       return true;
   }         
}
 app.config(function($stateProvider, $urlRouterProvider, $httpProvider){

 $stateProvider
.state('login', {
    url:'/login',
    templateUrl: 'templates/index/login/login.html',
    controller: 'loginCtrl'
 })

.state('dash', {
    url: '/dash',
    templateUrl:'templates/layout/dashboard.html',
    abstract:true
})

.state('dash.call',{
    url: '/call',
    templateUrl: 'templates/index/calls/calls.html',
    controller: 'callCtrl'
})

 .state('dash.profile', {
 url: '/profile',
 templateUrl: 'templates/index/account/profile/profile.html',
 controller: 'profileCtrl'

 })


});

I would like the navbar hidden for some states like when a user is on the login state

At the headerctrl i have also tried

 $scope.shownavbar = function(){
        var state = $state.current;
        $log.info(state);

  }

This returns in the console:

angular.js:13708 Object {name: "", url: "^", views: null, abstract: true}
angular.js:13708 Object {name: "", url: "^", views: null, abstract: true}
angular.js:13708 Object {url: "/login", templateUrl:  "templates/index/login/login.html", controller: "loginCtrl", name: "login"}
angular.js:13708 Object {url: "/login", templateUrl: "templates/index/login/login.html", controller: "loginCtrl", name: "login"}
 angular.js:13708 Object {url: "/login", templateUrl: "templates/index/login/login.html", controller: "loginCtrl", name: "login"}

WHAT COULD BE THE PROBLEM

1
  • 1
    maybe you can use a $rootScope to save true or false, and in the path of run() use a function that see when the state is changing and hide it. Commented Jun 25, 2016 at 3:13

1 Answer 1

3

I can think in two ways to solve this.

First solution EDITED

inside your run() put a function to see when the state is changing and hide or show the navbar.

myApp.run(function ($rootScope, $state) {
    $rootScope.navbar = false;
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        if (toState.name === 'login') {//toState variable see the state you're going 
            $rootScope.navbar = false;
        } else {
            $rootScope.navbar = true;
        }
    });
});

and change in your ng-show="navbar"

Second solution

the second solution i can think is you use multiple views.

$stateProvider
    .state('login', {
        url: '/',
        views: {
            'navbar': {
                templateUrl: null,
                controller: null
            },
            'body': {
                templateUrl: "views/login.html",
                controller: 'LoginController'
            }
        }
    })
    .state('home', {
        url: '/home',
        views: {
            'navbar': {
                templateUrl: "views/navbar.html",
                controller: null
            },
            'body': {
                templateUrl: "views/inicio.html",
                controller: null
            }
        }
    });

and in your html views put something similar to this:

<div ui-view="navbar"></div>
<div ui-view="body"></div>
Sign up to request clarification or add additional context in comments.

5 Comments

The first solution is the best but doesnt work... After trying console.log($state.current) in the $rootScope.$on('stateChangeStart..... ) block it returns null but console.log($state) returns something...
i'm so sorry i've tested it and it have a fews changes, please see my update
Now it works thanks.. Could you also add on how to handle the check of state using switch statement instead of if(..) else...
you can use it as same as a numer, please check this. stackoverflow.com/questions/2896626/…
Thanks for the help

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.