在角度滤波器模块的ng-repeat上使用groupBy

时间:2016-02-04 14:35:35

标签: javascript angularjs

如何在过滤器模块的ng-repeat上使用groupBy?

我有这个:

<div ng-init="fields = session.getCountry()">

    <span ng-repeat="(country, value) in fields | groupBy: 'country.code'" >
        <span ng-bind="country.code"></span>
        <span ng-bind="country.name"></span>
        <span ng-if="$last === false">, </span>
    </span>

</div>

session.getCountry()返回:

[
{
    code: "1",
    country: {
        code: "au",
        name: "Australia",
    },
    name: "Free"
},
{
    code: "2",
    country: {
        code: "au",
        name: "Australia",
    },
    name: "Pay"
},
{
    code: "3",
    country: {
        code: "us",
        name: "United States",
    },
    name: "Free"
},
]

我想有这个:

au Australia, us united States

但我的结果是:

au fi mx

我使用angular-filter v0.5.7

  

解决

我用:

<div ng-init="fields = session.getCountry()">
    <span ng-repeat="field in fields | unique: 'country.code'" >
        <span ng-bind="field.country.code"></span>
        <span ng-bind="field.country.code"></span>
        <span ng-if="$last === false">, </span>
    </span>
</div>

2 个答案:

答案 0 :(得分:0)

尝试像这样编写ng-repeat:

<div ng-repeat="(key, value) in fields  | groupBy:'country.code' ">

答案 1 :(得分:0)

首先,您应该尝试制作这样的“国家/地区”项目:

{
    code: "1",
    country: {
        code: "au",
        name: "Australia",
    },
    name: "Free"
},

只需更改“[”和“]”括号。

然后你可以这样做:

<span ng-repeat="field in fields | groupBy: 'field.country.code'" >
{{ field.country.code }}

快乐的编码!