Seglespaan Seglespaan - 6 months ago 186
AngularJS Question

angular.js using ng-options to only display unique values

If I have an array

$scope.colors = [
{name:'black', shade:'dark'},
{name:'white', shade:'light'},
{name:'red', shade:'dark'},
{name:'red', shade:'dark'},
{name:'yellow', shade:'light'}


];

Is it possible to use ng-options to build a select element with only unique values in the dropdown, so red will only display once?

Answer

AngularUI has exactly what you need, the ´unique´ filter (src code).

Example:

ng-options="color in colors | unique:'name'"
Comments