Ng-class不能切换class的开关
ng-class is not toggling class on/off
我有一个项目,应该在路径改变时打开/关闭类,但由于某种原因它不起作用。我从另一个项目复制并粘贴了它,它可以在那里工作,但由于某种原因它不能在这里工作。
这是控制器:
app.controller('AdminNav', function($scope, $location){
// Highlight navigation links
$scope.isActive = function(viewLocation){
var regexp = new RegExp(viewLocation + ".+");
return regexp.test($location.path());
};
});
这里是html:
<div class="collapse navbar-collapse" id="admin-side-nav" ng-controller="AdminNav">
<ul class="nav nav-pills nav-stacked">
<li ng-class="{active: isActive('/dashboard')}">
<a href="/dashboard"><i class="glyphicon glyphicon-home"></i> Home</a>
</li>
<li ng-class="{active: isActive('/dashboard/clicks')}">
<a href="/dashboard/clicks"><i class="glyphicon glyphicon-hand-up"></i> Clicks</a>
</li>
<li ng-class="{active: isActive('/dashboard/uploads')}">
<a href="/dashboard/uploads"><i class="glyphicon glyphicon-upload"></i> Uploads</a>
</li>
<li ng-class="{active: isActive('/dashboard/forms')}">
<a href="/dashboard/forms"><i class="glyphicon glyphicon-list"></i> Forms</a>
</li>
</ul>
</div>
当我点击其中一个链接的url改变,但active
类停留在第一个li
,我不知道为什么…任何想法吗?
这里是所有的javascript:
var app = angular.module('EDAdmin', ['ngRoute']);
app.config(function($routeProvider, $locationProvider){
// Prepare for html5 mode
$locationProvider.hashPrefix('!');
$locationProvider.html5Mode(true);
// Setup routing
$routeProvider.when('/dashboard/', {
templateUrl: '/templates/dashboard/default.html'
}).when('/dashboard/clicks', {
templateUrl: '/templates/dashboard/default.html'
}).when('/dashboard/uploads', {
templateUrl: '/templates/dashboard/default.html'
}).when('/dashboard/forms', {
templateUrl: '/templates/dashboard/default.html'
}).otherwise({
redirectTo: '/dashboard'
});
});
app.controller('AdminNav', function($scope, $location){
$scope.active = '';
// Highlight navigation links
$scope.isActive = function(viewLocation){
var regexp = new RegExp(viewLocation + ".+");
return regexp.test($scope.active);
};
$scope.$on('$routeChangeSuccess', function(){
$scope.active = $location.path();
})
});
问题是您构造了不正确的正则表达式。例如,当您单击第二项时,正则表达式将为
/'/dashboard'/clicks.+/
而位置路径为
/dashboard/clicks
显然,因为.+
的regexp不匹配给定的路径,但确实匹配/dashboard
。
更可靠的解决方案是使用当前$route.current
对象的regexp
属性,它被Angular内部用于这个目的——route to location path mapping:
$scope.isActive = function(viewLocation) {
if ($route.current && $route.current.regexp) {
return $route.current.regexp.test(viewLocation);
}
return false;
};
如果你为/dashboard/clicks
路由记录$route.current.regexp
,你会看到它看起来像这样:
/^'/dashboard'/clicks$/
所以你可以看到区别-你需要将字符串匹配限制为字符串的^
开始和$
结束。
http://plnkr.co/edit/7N0MQDYNWjo18bcmfIDB?p=preview
检查$scope.isActive
方法返回的值
检查柱塞
相关文章:
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 当浏览器上的后退按钮到达主页时,我需要删除Class
- 通过id和class属性获取元素
- AngularJS ng开关或类似的动态值工作
- 基于下拉菜单创建开关
- Javascript开关不工作
- 菜单栏class=活动引导程序主题无法正常工作
- $(.class).empty总是缺少一个元素
- jQuery[button.class]未检测到用按钮追加行
- 引导开关不工作
- javascript点击函数不;不适用于ID和Class
- HTML class=Ajax操作,如何让类点击调用好的操作
- Add a class if var < 0 jquery
- 将鼠标悬停在Angular JS中的一个元素上,并将Class添加到另一个元素中
- 如何定义星期日和平日.按日期使用开关
- Difference between methods of defining JavaScript 'class
- todomvc Backbone's更改为class'编辑'使todo可编辑
- 使用jQuery隐藏和显示具有相同Class的类
- Javascript-使用“”将toDateStringMonth从文本字符串解析为数字字符串;如果“;vs“;开关”;
- Ng-class不能切换class的开关