【问题标题】:ngRepeat:dupes error, although there is no duplicationngRepeat:dupes 错误,虽然没有重复
【发布时间】:2015-11-20 16:58:35
【问题描述】:

我有一个类似下面的 HTML 代码,我在其中使用了 AngularJS 框架:

<select name="choose-staff" ng-model="admin_times[0].user"  ng-change="update(reserve.staff)" id="choose-staff">
     <option ng-repeat="value in staff | unique:'employee.user.username'" value="{[{ value.employee.user.username }]}">{[{ value.employee.user.first_name }]} {[{ value.employee.user.last_name }]}</option>
</select>

我收到这样的错误:

Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: value in staff | unique:'employee.user.username', Duplicate key: string:ب, Duplicate value: ب

如果我使用track by $index,它会破坏我想要的结构,当我点击其中一个选项时,其余的就会消失。

[
    {
        "employee": {
            "user": {
                "id": 3,
                "first_name": "اشکان",
                "last_name": "وکیلی",
                "user_profile": null,
                "username": "ashkan"
            },
            "business": {
                "id": "caf241cd-adb4-44ee-8c40-0f6cdb3bc5ac",
                "fa_name": "ساینا",
                "en_name": "Saina",
                "service": [],
                "persian_address": "",
                "location": "35.77885523664743,51.39051060551765",
                "avatar": null,
                "email": ""
            },
            "is_head": true
        },
        "service": {
            "en_title": "Haircut",
            "fa_title": "کوتاهی مو"
        },
        "allocated_time": 60,
        "booked_no": "XG4OCX81"
    },
    {
        "employee": {
            "user": {
                "id": 3,
                "first_name": "اشکان",
                "last_name": "وکیلی",
                "user_profile": null,
                "username": "ashkan"
            },
            "business": {
                "id": "caf241cd-adb4-44ee-8c40-0f6cdb3bc5ac",
                "fa_name": "ساینا",
                "en_name": "Saina",
                "service": [],
                "persian_address": "",
                "location": "35.77885523664743,51.39051060551765",
                "avatar": null,
                "email": ""
            },
            "is_head": true
        },
        "service": {
            "en_title": "Color",
            "fa_title": "رنگ مو"
        },
        "allocated_time": 25,
        "booked_no": "1AY3F24G"
    },
    {
        "employee": {
            "user": {
                "id": 2,
                "first_name": "رضا",
                "last_name": "ولیمرادی",
                "user_profile": {
                    "id": "9d9be03a-f840-46ea-a21e-76cd5775a886",
                    "avatar": null,
                    "city": "",
                    "gender": "F",
                    "birthday": null,
                    "country": "IR",
                    "about": "",
                    "timestamp": "2015-11-06T14:56:10.312340Z",
                    "location": "36.03133177633187,51.328125"
                },
                "username": "reza"
            },
            "business": {
                "id": "caf241cd-adb4-44ee-8c40-0f6cdb3bc5ac",
                "fa_name": "ساینا",
                "en_name": "Saina",
                "service": [],
                "persian_address": "",
                "location": "35.77885523664743,51.39051060551765",
                "avatar": null,
                "email": ""
            },
            "is_head": false
        },
        "service": {
            "en_title": "Yellow",
            "fa_title": "زرد"
        },
        "allocated_time": 15,
        "booked_no": "H989M93X"
    },
    {
        "employee": {
            "user": {
                "id": 1,
                "first_name": "علیرضا",
                "last_name": "غفاری",
                "user_profile": {
                    "id": "884b36e3-7bad-466f-afee-25801572b834",
                    "avatar": null,
                    "city": "",
                    "gender": "F",
                    "birthday": null,
                    "country": "IR",
                    "about": "",
                    "timestamp": "2015-11-06T14:56:39.522362Z",
                    "location": "32.24997445586331,53.26171875"
                },
                "username": "alireza"
            },
            "business": {
                "id": "caf241cd-adb4-44ee-8c40-0f6cdb3bc5ac",
                "fa_name": "ساینا",
                "en_name": "Saina",
                "service": [],
                "persian_address": "",
                "location": "35.77885523664743,51.39051060551765",
                "avatar": null,
                "email": ""
            },
            "is_head": true
        },
        "service": {
            "en_title": "Color",
            "fa_title": "رنگ مو"
        },
        "allocated_time": 20,
        "booked_no": "O5KLFPZB"
    }
]

我不喜欢employee.user.username的冗余

【问题讨论】:

  • 前两个对象看起来一样...
  • 是的,但我使用了 '|唯一的:'employee.user.username'

标签: javascript json angularjs angularjs-ng-repeat


【解决方案1】:

在我看来,每个对象的唯一值是 booked_no

在这种情况下你应该使用track by value.booked_no

【讨论】:

  • TNX,但我不喜欢employee.user.username的冗余
  • employee.user.username 不是唯一的,这就是您收到错误的原因。当您获得该 JSON(来自服务器或其他)时,您可以遍历响应中的项目并建立一个唯一用户对象的列表。然后从该列表中执行ng-repeat,而不是完整的 JSON。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 2018-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
相关资源
最近更新 更多