简体   繁体   English

angularjs ui-router是当前状态活动的

[英]angularjs ui-router is current state active

I'm creating a menu in controller. 我在控制器中创建一个菜单。

$scope.menu = [
    {
        "title": "Home",
        "state": "app.home",
        "active": $state.is("app.home")
    },
    {
        "title": "Product",
        "state": "app.product",
        "active": $state.is("app.product")
    },
    {
        "title": "Category",
        "state": "app.category",
        "active":  $state.is("app.category")
    }
]

I set a property to specify active state. 我设置了一个属性来指定活动状态。 If my state is current, active property will be true. 如果我的州是最新的,那么有效财产将是真实的。

        <ul class="nav nav-pills nav-stacked">
             <li ng-repeat="item in menu" ng-class="{active:item.active}">
                <a ui-sref="{{item.state}}"> {{item.title}}</a>
            </li>
        </ul>

But when I change state, active property is not firing. 但是当我改变状态时,活动属性不会被触发。

Your suggested solution correction: 建议的解决方案更正:

<ul class="nav nav-pills nav-stacked">
             <li ng-repeat="item in menu" ng-class="{active: $state.is('stateName')}">
                <a ui-sref="{{item.state}}"> {{item.title}}</a>
            </li>
 </ul>

Or you may simply use ui-sref-active : 或者你可以简单地使用ui-sref-active

Example

Given the following template: 给出以下模板:

<ul>
  <li ui-sref-active="active" class="item">
    <a href ui-sref="app.user({user: 'bilbobaggins'})">@bilbobaggins</a>
  </li>
</ul>

When the app state is "app.user" (or any children states), and contains the state parameter "user" with value "bilbobaggins", the resulting HTML will appear as (note the 'active' class): 当app状态为“app.user”(或任何子状态),并且包含值为“bilbobaggins”的状态参数“user”时,生成的HTML将显示为(注意'active'类):

<ul>
  <li ui-sref-active="active" class="item active">
    <a ui-sref="app.user({user: 'bilbobaggins'})" href="/users/bilbobaggins">@bilbobaggins</a>
  </li>
</ul>

The class name is interpolated once during the directives link time (any further changes to the interpolated value are ignored). 在指令链接时间内对类名进行插值一次(忽略对插值的任何进一步更改)。

Multiple classes may be specified in a space-separated format: 可以以空格分隔格式指定多个类:

<ul>
  <li ui-sref-active='class1 class2 class3'>
    <a ui-sref="app.user">link</a>
  </li>
</ul>

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

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