关于angular的$state.include()方法与ui-sref和ui-sref-active的使用或者关于angular激活状态的判断
2017-08-22 16:27
621 查看
1.第一个方法:判断激活状态的方法就是$state.include();
$state.includes方法用于判断当前激活状态是否是指定的状态或者是指定状态的子、父状态.$state.includes方法接受三个参数,其中第二和第三个都不知道是干啥的...估计也不太用得到,就暂时不管了...
eg:$state.includes(stateOrName,params,options);
stateOrName:字符串,
是一个状态的名字(一般是指路由状态名字).
比如当前的激活状态是 "contacts.details.item"
如下调用:
$state.includes("contacts"); //返回true $state.includes("contacts.details"); //返回true $state.includes("contacts.details.item"); //返回true $state.includes("detail"); //返回undefined $state.includes("item"); //返回undefined
也可以使用glob语法:
$state.$current.name = 'contacts.details.item.url'; $state.includes("*.details.*.*"); // returns true $state.includes("*.details.**"); // returns true $state.includes("**.item.**"); // returns true $state.includes("*.details.item.url"); // returns true $state.includes("*.details.*.url"); // returns true $state.includes("*.details.*"); // returns undefined $state.includes("item.**"); // returns undefined
可以用于激活某个tab,让当前项高亮显示:
<li ng-class="{active:state.includes('dashboard.report')}"><a ui-sref="dashboard.report">Reports</a></li>
需要注意的是,在表达式里直接用$state是不行的,需要在控制器中把$state赋值给$scope下的变量.这样在表达式里才能使用(本人用的时候就是不知道这一点浪费了很多时间),active是指:state.includes('dashboard.report')返回为true时添加类名“active”;
$stateProvider.state('dashboard',{ url:'/dashboard', templateUrl:'./tpls/dashboard.html', controller:function($scope,$state){ $scope.state = $state; } })
实现的结果如下:
2.第二种方法:ui-sref和ui-sref-active的结合使用
ui-sref=“rank”表示点击跳转到rank的状态页面去,同时让状态处于激活状态,ui-sref-active就是检测是否处于激活状态,eg:ui-sref-active=“cur”;如果返回为true就自动添加class类cur;例如:<div class="top_nav"> <ul> <li ui-sref-active="on"> <a href="" ui-sref='main'>推荐</a> </li> <li ui-sref-active="on""> <a ui-sref='class'>分类</a> </li> <li class="" ui-sref-active="on"> <a ui-sref='bookbase'>书库</a> </li> <li ui-sref-active="on"> <a ui-sref='rank'>排行</a> </li> <li ui-sref-active="on"> <!--<i class="topnav-hot"></i>--> <a ui-sref="pay">充值</a> <!-- <i class="topnav-new"></i>--> <!-- <a href="?r=">人气</a>--> </li> </ul> </div>
然后在css中写相应的。cur对应的样式即可(省略)
实现的结果:
注意:这个只能实现点击当前页面才会激活,才会有相应的类名。才会实现想要的效果;如果不满足条件的时候就可以结合上面两点使用;
3.第三种方法:结合上面的两种方法:不解释,直接上代码:
html部分:
<div class="yw_tabbar" id="yw_tabbar"> <a ui-sref="main" class="yw_tabbar_item" ui-sref-active="cur" ng-class="{cur:state.includes('class')||state.includes('bookbase')||state.includes('rank')}"> <div class="yw_tabbar_icon yw_tabbar_icon_booklibs"></div> <p class="yw_tabbar_label">书城</p> </a> <a ui-sref="bookstore" class="yw_tabbar_item" ui-sref-active="cur"> <div class="yw_tabbar_icon yw_tabbar_icon_mybooks"></div> <p class="yw_tabbar_label">书架</p> </a> <a ui-sref="my" class="yw_tabbar_item" ui-sref-active="cur"> <div class="yw_tabbar_icon yw_tabbar_icon_user"></div> <p class="yw_tabbar_label">我的</p> </a> </div>js部分:
'use strict'; angular.module('app').directive('appFoot',[function(){ return{ restrict:'A', replace:true, templateUrl:'view/template/foot.html', controller:function($scope,$state){ $scope.state=$state; } } }])实现的效果如下:
到此结束。
相关文章推荐
- 关于Entity Framework更新的几种方式以及可能遇到的问题(附加类型“Model”的实体失败,因为相同类型的其他实体已具有相同的主键值)在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值,则可能会发生上述行为
- 关于Jquery使用is判断元素显示visible或者隐藏hidden失败
- Wix 使用总结(续)--关于Feature和Component的状态判断安装过程(转)
- 转载自新浪博客关于 asp或者SHTML 中 # include file/virtual的使用区别
- 关于windows激活状态不可用,software protection服务启动时提示找不到文件的解决方法
- C#使用Ping功能判断网络连接状态或者目标计算机是否开机等
- 关于判断字符串参数是否是整型参数或者浮点型参数的方法
- 关于Genymotion使用android5.1报错或者无法启动的解决方法
- 关于Jquery使用is判断元素显示visible或者隐藏hidden失败
- 关于使用注解出现BeanCreationException或者NameNotFoundException的解决方法
- 关于windows激活状态不可用,software protection服务启动时提示找不到文件的解决方法
- 关于主线程等待子线程退出状态的判断函数的使用说明
- 关于使用UIAlertView之后pop或者push时键盘闪退问题解决方法
- MongoDB的upsert状态判断和pymongo使用方法
- 关于 DataRow 中为 DataRowState.Deleted 状态的 字段列值取值方法
- 【Android 开发】:UI控件之开关状态按钮 ToggleButton 的使用方法
- 附加类型“UniversalReviewSystem.Models.ApplicationUser”的实体失败,因为相同类型的其他实体已具有相同的主键值。在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值
- 关于在二级目录下,Cookie不能删除或者使用的解决方法的
- 因为相同类型的其他实体已具有相同的主键值。在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 。。。
- 内核中判断返回指针是否错误的方法:使用IS_ERR或者IS_ERR_OR_NULL