根据 Angular 中的路线变化添加 Bootstrap 活动 class
add Bootstrap active class based on route change in Angular
我正在尝试根据路径在我的导航菜单上使用 Angular ngClass 添加 class(bootstrap 活动 class)。
导航主模板:
<div class="section">
<div class="container">
<div class="row">
<div class="col-md-12" ng-controller="MainCtrl">
<ul class="nav nav-pills nav-justified" style="width:30%">
<li ng-class="{active: location === '#!/home', nav-item}">
<a href="#!/home">Home</a>
</li>
<li ng-class="{active: location === '#!/resume', nav-item}">
<a href="#!/resume">CV</a>
</li>
<li ng-class="{active: location === '#!/overviewActivities', nav-item}">
<a href="#!/overviewActivities">I-Talent</a>
</li>
</ul>
</div>
</div>
</div>
app.module.js:
angular.
module('iTalentApp', [
'ngRoute',
'home',
'resume',
'navigationMain'
]).controller('MainCtrl', function ($scope, $rootscope, $location) {
$scope.location = $location.path();
$rootScope.$on('$routeChangeSuccess', function () {
$scope.location = $location.path();
});
});
index.html:
<body>
<navigation-main></navigation-main>
<div ng-view></div>
</body>
我的配置:
angular.
module('iTalentApp').
config(['$locationProvider', '$routeProvider',
function config($locationProvider, $routeProvider) {
$locationProvider.hashPrefix('!');
$routeProvider.
when('/home', {
template: '<home></home>'
}).
when('/resume', {
template: '<resume></resume>'
}).
when('/overviewActivities', {
template: '<overview-activities></overview-activities>'
}).
otherwise('/home');
}
]);
我试图实现这个问题的解决方案,但无济于事。谁能指出我做错了什么?
这就是我想要实现的目标,但是在带有 angular、http://plnkr.co/edit/FNwIAU4OgQHlREIfi4BG 的单个页面中,我得到了它的工作,除了条件激活 class 当它们被使用时。 (我对 html、css 和 js 也很陌生)
编辑:
错误信息:
angular.js:14199 Error: [$injector:unpr] Unknown provider: $rootscopeProvider <- $rootscope <- MainCtrl
http://errors.angularjs.org/1.5.11/$injector/unpr?p0=%24rootscopeProvider%20%3C-%20%24rootscope%20%3C-%20MainCtrl
at angular.js:68
at angular.js:4563
at Object.getService [as get] (angular.js:4716)
at angular.js:4568
at getService (angular.js:4716)
at injectionArgs (angular.js:4741)
at Object.invoke (angular.js:4763)
at $controllerInit (angular.js:10592)
at nodeLinkFn (angular.js:9469)
at compositeLinkFn (angular.js:8810)
既然我们发现了评论中的错误,我会花时间写一个答案以备将来参考,问题是:
- 控制器中的拼写错误,将 $rootscope 更改为 $rootScope,
- 错误使用 ng-class 语法,删除了 nav-item.
- ng-class 的表达错误,location==='#!/path' 变成了location==='/path'.
感谢@dfsq
我正在尝试根据路径在我的导航菜单上使用 Angular ngClass 添加 class(bootstrap 活动 class)。
导航主模板:
<div class="section">
<div class="container">
<div class="row">
<div class="col-md-12" ng-controller="MainCtrl">
<ul class="nav nav-pills nav-justified" style="width:30%">
<li ng-class="{active: location === '#!/home', nav-item}">
<a href="#!/home">Home</a>
</li>
<li ng-class="{active: location === '#!/resume', nav-item}">
<a href="#!/resume">CV</a>
</li>
<li ng-class="{active: location === '#!/overviewActivities', nav-item}">
<a href="#!/overviewActivities">I-Talent</a>
</li>
</ul>
</div>
</div>
</div>
app.module.js:
angular.
module('iTalentApp', [
'ngRoute',
'home',
'resume',
'navigationMain'
]).controller('MainCtrl', function ($scope, $rootscope, $location) {
$scope.location = $location.path();
$rootScope.$on('$routeChangeSuccess', function () {
$scope.location = $location.path();
});
});
index.html:
<body>
<navigation-main></navigation-main>
<div ng-view></div>
</body>
我的配置:
angular.
module('iTalentApp').
config(['$locationProvider', '$routeProvider',
function config($locationProvider, $routeProvider) {
$locationProvider.hashPrefix('!');
$routeProvider.
when('/home', {
template: '<home></home>'
}).
when('/resume', {
template: '<resume></resume>'
}).
when('/overviewActivities', {
template: '<overview-activities></overview-activities>'
}).
otherwise('/home');
}
]);
我试图实现这个问题的解决方案,
这就是我想要实现的目标,但是在带有 angular、http://plnkr.co/edit/FNwIAU4OgQHlREIfi4BG 的单个页面中,我得到了它的工作,除了条件激活 class 当它们被使用时。 (我对 html、css 和 js 也很陌生)
编辑: 错误信息:
angular.js:14199 Error: [$injector:unpr] Unknown provider: $rootscopeProvider <- $rootscope <- MainCtrl
http://errors.angularjs.org/1.5.11/$injector/unpr?p0=%24rootscopeProvider%20%3C-%20%24rootscope%20%3C-%20MainCtrl
at angular.js:68
at angular.js:4563
at Object.getService [as get] (angular.js:4716)
at angular.js:4568
at getService (angular.js:4716)
at injectionArgs (angular.js:4741)
at Object.invoke (angular.js:4763)
at $controllerInit (angular.js:10592)
at nodeLinkFn (angular.js:9469)
at compositeLinkFn (angular.js:8810)
既然我们发现了评论中的错误,我会花时间写一个答案以备将来参考,问题是:
- 控制器中的拼写错误,将 $rootscope 更改为 $rootScope,
- 错误使用 ng-class 语法,删除了 nav-item.
- ng-class 的表达错误,location==='#!/path' 变成了location==='/path'.
感谢@dfsq