简体   繁体   English

Angular Bootstrap活动菜单项

[英]Angular bootstrap active menu item

I have on my main page : 我在主页上:

  <li ng-class="{ active: hello.isActive('/page1')}"><a ui-sref="root.content({store:hello.store_selected,product:hello.product_selected})" >page1</a></li>
  <li ng-class="{ active: hello.isActive('/page2')}"><a ui-sref="root.turnover({store:hello.store_selected,product:hello.product_selected})">page2</a></li>

I have two controller for each route: Page1Controller and Page2Controller. 对于每个路由,我有两个控制器:Page1Controller和Page2Controller。 The main page where I pick this code from has also a controller: HelloController. 我从中选择此代码的主页上还有一个控制器:HelloController。 I have in my helloController 我在helloController中

  vm.isActive= function(viewLocation){
     return viewLocation ===$location.path();
   };

When I click on an item i get the current item highlighted. 当我单击一个项目时,我将当前项目突出显示。 But I have two problems : when i click anywhere else on the page the current item is not highlighted anymore. 但是我有两个问题:当我单击页面上的任何其他位置时,当前项目不再突出显示。 And also at the start of the page or when I refresh a page, i don't have highlighted item . 而且在页面开头或刷新页面时,我也没有突出显示item。 Could you help please ? 你能帮忙吗? Thank you 谢谢

ui-router already has this functionality built in using ui-sref-active ui-router已经使用ui-sref-active内置了此功能

<li ui-sfref-active="active"><a ui-sref="root.content({store:hello.store_selected,product:hello.product_selected})" >page1</a></li>

Your problem probably is that path isn't an absolute match and you should probably have been using indexOf() instead of checking for equality 您的问题可能是路径不是绝对匹配,您可能应该一直在使用indexOf()而不是检查是否相等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM