简体   繁体   English

使用ng-repeat在数组$ first项上的条件类

[英]Conditional class on $first item in array using ng-repeat

I am building an app that pulls and displays a list of time slots from a database using ng-repeat. 我正在构建一个使用ng-repeat从数据库中提取并显示时隙列表的应用。 I also have a navigation that pulls and displays the month/day from the same database using ng-repeat. 我也有一个导航,可以使用ng-repeat从同一数据库中提取并显示月份/日期。 When you click on one of the days in the nav, ng-click will filter the list of times that match the day chosen...this all works. 当您单击导航中的某一天时,ng-click将过滤与所选日期匹配的时间列表...这一切都可以。

My issue is having the first nav array item activated so the user will know which day they are starting from - the below screen is the first view and although th times below are for the first array item; 我的问题是激活了第一个导航阵列项,因此用户将知道他们从哪一天开始-下面的屏幕是第一个视图,尽管以下是第一个阵列项。 you wouldn't know because theres no class to it. 您不会知道,因为没有课程。 I was wondering if there is a way to conditionally set ng-class="$first ? 'active' : ''" until another link was clicked? 我想知道是否有办法有条件地设置ng-class="$first ? 'active' : ''"直到单击了另一个链接? Obviously just setting $first works, but it stays active even when choosing another date. 显然,仅设置$ first有效,但即使选择其他日期,它也保持活动状态。 Below is the code for the nav. 以下是导航的代码。 I am using angular-material for the ui and ui-router for my router. 我正在为ui使用ui-material,为路由器使用ui-router。 In the image below you'll notice that JAN FRI 30 is the first item displayed in the array. 在下图中,您会注意到JAN FRI 30是数组中显示的第一项。 Thank you for your help. 谢谢您的帮助。

<ul hide-gt-sm>
        <li ng-repeat="day in times">
          <md-button class="md-primary"
                      ui-sref-active="{{day.date}}"
                      ng-click="filterMobileTimes(day.date.fullDate)"
                      ng-class="$first ? 'active' : ''">
            {{day.date.month}}<br>{{day.date.day}}
          </md-button>
        </li>
      </ul>

在此处输入图片说明

out of the simples ways of achieving this are 实现这一目标的简单方法是

<ul hide-gt-sm ng-init="activeIndex=0">
    <li ng-repeat="day in times">
      <md-button class="md-primary"
                  ng-click="activeIndex=$index; filterMobileTimes(day.date.fullDate)"
                  ng-class={active:(activeIndex==$index)}>
        {{day.date.month}}<br>{{day.date.day}}
      </md-button>
    </li>
  </ul>

or w/o initializing 或不初始化

<ul hide-gt-sm>
    <li ng-repeat="day in times">
      <md-button class="md-primary"
                  ng-click="activeIndex=$index; filterMobileTimes(day.date.fullDate)"
                  ng-class={active:(activeIndex?activeIndex==$index:$first)}>
        {{day.date.month}}<br>{{day.date.day}}
      </md-button>
    </li>
  </ul>

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

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