在angular ui路由器中实现navbar嵌套视图

时间:2016-06-25 02:59:44

标签: javascript angularjs angular-ui-router

角度js的新手。 我实现了一个嵌套的ui视图,但问题是在检查页面的当前状态时它会返回许多对象,这样我就无法使用$ state.current来设置ng-show

我希望在某些州显示和隐藏导航栏。

我试过了

~/tools/android/android-ndk-r12/prebuilt/linux-x86_64/bin/gdb-orig:
error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

app.js代码

cd /usr/lib
sudo ln -s libncursesw.so.6.0 libncurses.so.5
sudo ln -s libncursesw.so.6.0 libtinfo.so.5

我希望某些状态隐藏导航栏,例如当用户处于登录状态时

在headerctrl我也试过

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>

在控制台中返回:

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'

 })


});

可能是什么问题

1 个答案:

答案 0 :(得分:3)

我可以用两种方式来解决这个问题。

第一个解决方案 EDITED

run()内放置一个功能,以查看状态何时发生变化并隐藏或显示导航栏。

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;
        }
    });
});

并更改ng-show="navbar"

第二个解决方案

我能想到的第二个解决方案是你使用多个视图。

$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
            }
        }
    });

并在您的html观点中添加了与此类似的内容:

<div ui-view="navbar"></div>
<div ui-view="body"></div>