3

I try displaying a select when the previous select was checked.

<div ng-repeat="item in collection">

<div ng-show="$index === 0 || $parent.list[$index].nom">
    <select ng-model="$parent.list[$index].nom" ng-options="..."></select>
</div>
  1. <div ng-repeat="item in collection">

I loop through collection and I create many selects that there are item in collection.

  1. <div ng-show="$index === 0 || $parent.list[$index].nom">

I want to display/hide the parent div of selects with two conditions :

  1. I show the div if the index is equal to 0 (for display first select)
  2. I show the div if the current ngModel contains the nom
  1. <select ng-model="$parent.list[$index].nom" ng-options="...">

I put a dynamic ngModel where each select has his own model like :

exemple of ngModel
(source: noelshack.com)

Test Exemple : I have three options in a select, so I want give opportunity to member to choose each option of the select.

If the member choose an option of select 1 the seconde select show on and If he select an option of second select the third select show on but no more else...

THE PROBLEME HERE : $index in the directive ngShow seem's known with this condition :

$index === 0

but no here :

$parent.list[$index].nom
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Kévin Vilela Pinto
  • 326
  • 1
  • 6
  • 21

2 Answers2

2

You must include track by $index in your ng-repeat...

<div ng-repeat="item in collection track by $index>
Maxwelll
  • 2,174
  • 1
  • 17
  • 22
1

I think it should be:

<div ng-show="$index === 0 || $parent.list[$index - 1].nom">
    <select ng-model="$parent.list[$index].nom" ng-options="el for el in els"></select>
</div>

Note, that you want to refer previous item in the list, hence $index - 1.

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Perferct, it work like a charm now. Thank's you ! By this way this is possible to generate options based on the previous list by removing the selected option ? – Kévin Vilela Pinto Feb 08 '16 at 14:52
  • Not sure I understand. Can you explain in more details what you want to achieve, so I can understand it better? – dfsq Feb 08 '16 at 14:56
  • For example, I have a list with two options. I select the first option of the first list. So the second list appears. This second list contain only one option, and this option hasn't been selected in previous select. You know what I mean ? – Kévin Vilela Pinto Feb 08 '16 at 15:08
  • So you don't want to show already selected names in previous selectboxes? – dfsq Feb 08 '16 at 15:22
  • And how does `list` look like? – dfsq Feb 08 '16 at 15:30
  • Right. Because I assume is useless to add a list with the already selected option names. – Kévin Vilela Pinto Feb 08 '16 at 15:31
  • 1
    @KévinVilelaPinto Take a look at this, should help you with your problem: http://jsfiddle.net/mknxnj84/11/ – dfsq Feb 08 '16 at 22:04
  • Thank's @dfsq ! Your job was almost perfect! I've done some corrections because when you changed the quantity of first select to pass it to zero. All next lists was removed. But now all working fine :) http://jsfiddle.net/mknxnj84/14/ – Kévin Vilela Pinto Feb 09 '16 at 10:35