Javascript AngularJ在某些路线上显示侧边栏,在其他路线上隐藏侧边栏
我试图利用ng if在我的一些单页应用程序页面中包含侧栏,并对其他页面隐藏它。html是:Javascript AngularJ在某些路线上显示侧边栏,在其他路线上隐藏侧边栏,javascript,angularjs,Javascript,Angularjs,我试图利用ng if在我的一些单页应用程序页面中包含侧栏,并对其他页面隐藏它。html是: <body ng-app="simpleapp"> <div id="wrapper"> <!-- Navigation --> <div ng-include="'./include/sidebar.html'"></div> <!-- Page
<body ng-app="simpleapp">
<div id="wrapper">
<!-- Navigation -->
<div ng-include="'./include/sidebar.html'"></div>
<!-- Page Content -->
<div id="page-wrapper">
<div class="container-fluid">
<div ng-view class="slide-animation"></div>
</div>
</div>
</div>
我找到的最佳解决方案是在页面主体中添加另一个ctrl键,并使用ng if和condition将id添加到div
<div ng-if="$root.show" ng-include="'./include/sidebar.html'"></div>
</sidebar>
<!-- Page Content -->
<div id="{{$root.show ? 'page-wrapper':''}}">
这很管用。。然而当页面发生变化时。。没有重新应用此条件:(因此,如果我打开#/login,我可以看到没有侧栏,但是一旦我移动到/home
,侧栏也不会显示
你能帮个忙吗?我试着找了几个小时都没找到
谢谢
< p>我没有看到任何东西使用你的代码> McCCTL/<代码>,所以我不确定它是如何被使用的。我想你想考虑的是一个更类似的方法:angular.module('myApp')
.run(function($rootScope, $location, $routeParams) {
$rootScope.$on('$routeChangeSuccess', function(event, current) {
// Look at $location.path()
// If it isn't what you want, toggle showSideBar...
$rootScope.showSideBar = true|false;
}
}]);
<div ng-if="showSideBar" ng-include="'./include/sidebar.html'"></div>
然后您的HTML可以这样引用它:
angular.module('myApp')
.run(function($rootScope, $location, $routeParams) {
$rootScope.$on('$routeChangeSuccess', function(event, current) {
// Look at $location.path()
// If it isn't what you want, toggle showSideBar...
$rootScope.showSideBar = true|false;
}
}]);
<div ng-if="showSideBar" ng-include="'./include/sidebar.html'"></div>