admin管理员组文章数量:1180477
have an angular application that I want to hide one of the 'included views' if the main view is the homepage view.
<div id="page" ng-class="{ showNav: $root.showNav }">
<header id="pageHeader" ng-controller="HeaderCtrl" data-ng-include="'views/includes/header.html'"></header>
<div id="pageHero" ng-show='$rootScope.fullView' ng-controller="MainsearchCtrl" data-ng-include="'views/mainSearch.html'"></div>
<div id="pageContent" ng-view=""></div>
</div>
have an angular application that I want to hide one of the 'included views' if the main view is the homepage view.
<div id="page" ng-class="{ showNav: $root.showNav }">
<header id="pageHeader" ng-controller="HeaderCtrl" data-ng-include="'views/includes/header.html'"></header>
<div id="pageHero" ng-show='$rootScope.fullView' ng-controller="MainsearchCtrl" data-ng-include="'views/mainSearch.html'"></div>
<div id="pageContent" ng-view=""></div>
</div>
Share
Improve this question
edited Mar 11, 2014 at 23:41
silvster27
asked Mar 11, 2014 at 23:17
silvster27silvster27
1,9366 gold badges31 silver badges44 bronze badges
3 Answers
Reset to default 21You can inject either $route
or $location
into your controller, fetch needed value from one of these services and use it in ng-show
or ng-if
.
Example of using $route
and $location
you can see here.
Here is one of possible ways of doing it:
JavaScript
angular.module('app', ['ngRoute']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/one', {
controller: 'routeController',
templateUrl: 'routeTemplate.html'
}).
when('/two', {
controller: 'routeController',
templateUrl: 'routeTemplate.html'
}).
otherwise({
redirectTo: '/one'
})
}]).
controller('routeController', ['$scope', '$location', function($scope, $location) {
$scope.showPageHero = $location.path() === '/one';
}]);
routeTemplate.html
<div>
<h1>Route Template</h1>
<div ng-include="'hero.html'" ng-if="showPageHero"></div>
<div ng-include="'content.html'"></div>
</div>
Plunker: http://plnkr.co/edit/sZlZaz3LQILJcCywB1EB?p=preview
Just use ng-show with a negative expression:
<div id=includedView ng-view="included" ng-show="location != '/main'"></div>
You'll have to set the value of location
on your controller $scope
in your controller; possibly using the $route
or $location
providers.
If you use routes you can run some logic on each route change in the resolve
block in the routeprovider.
In the example below I'm using a custom SystemService service that stores the location and in turn broadcasts an event on $rootScope. In this example the navigation is outside of the views managed by the routed controllers and has its own controller:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/welcome.html',
controller: 'WelcomeCtrl',
resolve: {
load: function(SystemService){
SystemService.fireNavEvent({showNav:false,location:'/'});
}
}
}).
when('/other', {
templateUrl: 'partials/other.html',
controller: 'ElseCtrl',
resolve: {
load: function(SystemService){
SystemService.fireNavEvent({showNav:true,location:'/other'});
}
}
}).
otherwise({
redirectTo: '/'
});
}]);
// in SystemServce:
function fireNavEvent(obj){
this.showNav = obj.showNav;
$rootScope.$broadcast('navEvent',obj);
}
// then in the navigtion controller:
$scope.$on("navEvent", function(evt,args){
$scope.showNav = SystemService.showNav;
// also some logic to set the active nav item based on location
});
There are other ways to achieve this, for example you could just broadcast the navigation change on $rootScope
directly from the routes config.
You could also inject $location into your controller and set the visibility based on that.
本文标签: javascriptHow do I use angular nghide based on the pageroute that I am currently onStack Overflow
版权声明:本文标题:javascript - How do I use angular ng-hide based on the pageroute that I am currently on? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738139683a2065629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论