首页 技术 正文
技术 2022年11月23日
0 收藏 665 点赞 3,536 浏览 968 个字

在传统jQuery中,实现导航栏动态添加.active类的思路比较简单,就是当点击的时候,清除其他.active,然后给当前类加上.active。

但是在AngularJS中,就不能再采用这种jQuery思维来实现了。

思路如下:

  1、定义items,存储导航栏的内容

  2、在html中,用ng-repeat 指令去变量items,输出导航栏的内容

  3、用ng-class{‘active’, isActive(item.href)} 指令去判断当前的url与item.href是否相等,相等则加上.active

  4、在controller的函数中 isActive(href)去判断,如果二者相等返回true

JS代码

 .controller('HeaderCtrl', ['$scope','$location', function ($scope,$location) {
$scope.items = [{
id: '1', name: '首页', href: '#/'
},{
id: '2', name: '活期', href: '#/product/tmb'
},{
id: '3', name: '定期', href: '#/product/dcb'
},{
id: '4', name: '添牛', href: '#/product/tnb'
},{
id: '5', name: '安全', href: '#/security'
},{
id: '6', name: '账户', href: '#/profile'
}] $scope.selected = 1;
$scope.isActive = function (current) {
var href = '#'+$location.url();
return current === href;
}
}])

HTML代码

<ul id="nav1" class="nav nav-pills" role="tablist">
<li role="presentation" ng-repeat="item in items">
<div class="header_home" ng-class="{'active':isActive(item.href)}">
<a ng-href="{{item.href}}" rel="external nofollow" ><span class="h4">{{item.name}}</span></a>
</div>
</li>
</ul>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,992
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,767
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844