【发布时间】:2016-10-27 17:31:05
【问题描述】:
我是 Angular js 的新手。 我正在实现一个嵌套的 ui 视图,但问题是在检查页面的当前状态时它返回许多对象,因此我不能使用 $state.current 来设置 ng-show
我希望在某些状态下显示和隐藏导航栏。
我试过了
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>
app.js 代码
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'
})
});
我希望在某些状态下隐藏导航栏,例如当用户处于登录状态时
headerctrl 我也试过了
$scope.shownavbar = function(){
var state = $state.current;
$log.info(state);
}
这会在控制台中返回:
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"}
可能是什么问题
【问题讨论】:
-
也许您可以使用
$rootScope来保存真假,并在run()的路径中使用查看状态何时发生变化并将其隐藏的函数。
标签: javascript angularjs angular-ui-router