1

How can I use ng-options to populate a select element with the results of the "categories" arrays, that are within the objects?

        {
          title: "Star Wars",
          categories: ["Coding", "Exploration", "Adventure"]
        }, {
          title: "Street Wars",
          categories: ["Surfing", "Exploration", "Testing"]
        },

I want to populate a select element with all the available categories, and then use "unique" to filter out the duplicate entries...

0

2 Answers 2

2

As the comment says, it's probably easier to create the unique part in the controller, and use ngOptions on the new array:

$scope.uniqueOptions = [];
for (var i = 0; i < data.length; i++) {
    for (var j = 0; j < data[i].categories.length; j++) {
        if ($scope.uniqueOptions.indexOf(data[i].categories[j]) === -1) {
            $scope.uniqueOptions.push(data[i].categories[j])
        }
    }
}

And the options:

<select ng-model="someModel" ng-options="category as category for category in uniqueOptions"> </select>
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it this way provided you are using an ngRepeat

 <select ng-model="cat" ng-options="c for c in movie.categories"></select>

This is a working example.

EDIT: ngModel is required for select.

1 Comment

I think the controller is a better place to handle the data - voted your answer up because it does work also... Thank you @mitch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.