Lio Programista
Lio Programista

Reputation: 163

Pass different array every time in ng-repeat

I am generating comboboxes dynamically and I need to pass a different collection to the ng-repeat every time.How can I do that?

<div ng-repeat="choice in $ctrl.inputFilterRows">
    <md-select ng-model="choice.name">
        <md-option ng-repeat="filter in $ctrl.filters" value="{{filter.value}}" >
        {{filter.value}}
       </md-option>
    </md-select>
</div>

Tried to set from the controller, but didnt work:

self.inputFilterRows[0].filters = [{ value: 'June' }, { value: 'July' }, { value: 'August' }];

Upvotes: 0

Views: 36

Answers (1)

Dimitrios Matanis
Dimitrios Matanis

Reputation: 926

An idea would be to use ng-if on several md-select elements and decide which one to enable based on a condition that suits you.

Another is to have a $scope variable that is linked to a single ng-repeat select, but you keep assigning new values to that $scope variable collection whenever you want. That would force a scope redraw and the ng-repeat would now use the new collection values.

Second one is probably cleaner.

EDIT:

Based on a better explanation provided in the comments below I now realise that you want a set of selects, each with their own set of options.

To achieve something like that I would suggest having an array of arrays, in which each object would represent a select, and then it's contents would be the options for that select.

$scope.selectArray = [
  { name: 'colours', filters: [{ value: 'black' }, { value: 'red' }, { value: 'blue' }] },
  { name: 'months', filters: [{ value: 'January' }, { value: 'February' }, { value: 'March' }] }
];

Now, you can have an ng-repeat iterating over selectArray (select in selectArrays) to create the selects, and then each one would contain another ng-repeat to iterate over the select.filters (filter in select.filters)

I am not going to write the exact code because you look like you know what you're doing and I'm sure you can put it together yourself very easily.

If you want to change the dataset of a specific select you can do something like:

$scope.selectArray[1].filters[0].value = 'December';

or

$scope.selectArray[1].filters = [{ value: 'June' }, { value: 'July' }, { value: 'August' }];

Upvotes: 1

Related Questions