我刚开始玩Angular.js,并且对ngOptions有个疑问:是否可以标记optgroup?Angular.js用ngOptions选择:标记optgroup
让我们假设2个物体 - 汽车和车库。
cars = [
{"id": 1, "name": "Diablo", "color": "red", "garageId": 1},
{"id": 2, "name": "Countach", "color": "white", "garageId": 1},
{"id": 3, "name": "Clio", "color": "silver", "garageId": 2},
...
]
garages = [
{"id": 1, "name": "Super Garage Deluxe"},
{"id": 2, "name": "Toms Eastside"},
...
]
有了这个代码,我几乎是结果,我想:
ng-options = "car.id as car.name + ' (' + car.color + ')' group by car.garageId for car in cars"
导致选择:
-----------------
1
Diablo (red)
Countach (white)
Firebird (red)
2
Clio (silver)
Golf (black)
3
Hummer (silver)
-----------------
但我要标注如 “车库1” 的optgroups, “车库2”,......甚至更好地显示车库的名称,而不仅仅是garageId。
的angularjs.org documentation for select只字未提标签的OPTGROUP,但我想通过ngOptions的一部分,像group by car.garageId as 'Garage ' + car.garageId
或group by car.garageId as getGarageName(car.garageId)
延长集团 - 这可悲的是不工作。
到目前为止,我唯一的解决方案是添加一个新的属性“garageDisplayName”到汽车对象,并在那里存储ID +车库名称并将其用作参数组。但是,当车库名称改变时,我不想更新所有车辆。
有没有办法用ngOptions标签optgroups,或者我应该在这种情况下使用ngRepeat?
非常感谢!我总是用'as'部分尝试它,而不是只写我想要显示的表达式。 :)所以也可以通过'Garage'+ car.garageId'进行分组。 –
而且正如你所建议的,我认为存储车库的参考而不是id,并以此结束:'group by car.garage.name +' - '+ car.garage.id' _(车库名称不唯一)_这确实更简单。 –