ngRepeat:重复数据消除错误,尽管没有重复

ngRepeat:dupes error, although there is no duplication

本文关键字:数据 ngRepeat 错误      更新时间:2023-09-26

我有一个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 的冗余

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

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