2

I have a HTML code like below which I use AngularJS framework within it:

<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>

And I get an error like this:

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: ب

And if I use track by $index it destroys my desired structure and when I click one of options, the rest of them get vanished.

[
    {
        "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"
    }
]

I don't like the redundancy of employee.user.username

Alireza Ghaffari
  • 1,004
  • 3
  • 11
  • 24

1 Answers1

0

Looks to me like the unique value in each object is the booked_no.

In which case you should use track by value.booked_no

Starscream1984
  • 3,072
  • 1
  • 19
  • 27
  • TNX, but I don't like the redundancy of employee.user.username – Alireza Ghaffari Nov 20 '15 at 17:18
  • `employee.user.username` isn't unique, which is why you're getting the error. When you get that JSON (from server or whatever), you could loop through the items in the response and build up a list of unique user objects. Then do `ng-repeat` from that list, not your full JSON. – Starscream1984 Nov 20 '15 at 17:23