【问题标题】:using $index to compare values returned by ng-repeat使用 $index 比较 ng-repeat 返回的值
【发布时间】:2016-10-10 20:23:07
【问题描述】:

我有一个大小为 2 的数组对象。我正在对数组对象执行 ng-repeat 并显示值。但现在我还想比较对象内部的值。我可以为此使用 $index 吗?

我尝试使用 $index, $index-1 但它不起作用。事实上,它根本不会显示任何值。

div.timeline-block(ng-repeat="(key,value) in vm.alertHistoryData | groupBy: 'historyDateWithoutTime' | toArray:true | orderBy: 'historyDateWithoutTime':true " )
        h2.timeline-block-header
            | {{ value[0].historyDateWithoutTime | date: "mediumDate" }}
        ul.timeline-entry-list
            //- to make the array of data render in reverse order we add "true" to orderBy filter
            li.timeline-entry-list-item(ng-repeat= "history in value | orderBy: 'alert-modified-date':true")
                h3.list-item-time
                    | {{ history["alert-modified-date"] | date:"mediumTime"}}
                div.list-item-content
                    div.label-block.label-block-horz
                        | State
                        div.label-block-value
                            | {{ history.state[$index] | alertState }}
                        hr.hr-default.hr-spacer-sm
                        div.label-block-value
                            | {{ history[$index-1].state }}
                    div.label-block.label-block-horz
                        | Priority:
                        div.label-block-value
                            span.label-priority(ng-class="'priority-{{history.priority}}'")
                                | {{ history.priority}}

是不是因为我也在使用过滤器?请帮忙

【问题讨论】:

    标签: angularjs json


    【解决方案1】:

    historyvalue 中的一个项目。

    变化:

    history[$index-1].state
    

    收件人:

    value[$index-1].state
    

    【讨论】:

    • 成功了 :) 非常感谢。你能解释一下我做错了什么吗?
    • 例如:value = [{state: 'a'}, {state: 'b'},{state: 'c'}]。使用ng-repeat="history in value,我们首先得到history 是{state: 'a'}。你不能选择历史[$index].state。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多