0

I am trying to write the following code with two ng-repeat.

<td class="researched" colspan="2" ng-repeat="tech in userCtrl.chosenTechs1" title="{{tech.name}}">{{tech.name}}</td>
<td class="available" colspan="2" title="6 trade needed to learn level 1 tech" ng-if="5 > userCtrl.chosenTechs1.length" >6</td>
<td class="available" colspan="2" title="6 trade needed to learn level 1 tech" ng-if="4 > userCtrl.chosenTechs1.length" >6</td>
<td class="available" colspan="2" title="6 trade needed to learn level 1 tech" ng-if="3 > userCtrl.chosenTechs1.length" >6</td>
<td class="available" colspan="2" title="6 trade needed to learn level 1 tech" ng-if="2 > userCtrl.chosenTechs1.length" >6</td>
<td class="available" colspan="2" title="6 trade needed to learn level 1 tech" ng-if="1 > userCtrl.chosenTechs1.length" >6</td>

I have tried this, but didn't work

<td class="researched" colspan="2" ng-repeat="tech in userCtrl.chosenTechs1" title="{{tech.name}}">{{tech.name}}</td>
<td class="available" colspan="2" title="6 trade needed to learn level 1 tech" ng-repeat="5 > $index">6</td>

Bascially what I want is to get the length of userCtrl.chosenTechs1, and if the length is less than 5, I want to repeat it until it reaches 5

Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
  • You are ok with $index but in the other tds you are out of the scope . So you have to change your logic . – katmanco Feb 22 '15 at 21:10

2 Answers2

1
ng-repeat="5 > $index"

This is a bad approach. You need to have another list/array to iterate. Prepare the second array in the controller, make it empty in case the length of the first one is greater than or equal to 5, so it will disappear in that case. You shouldn't include too much magic in what data to show in the html, that logic belongs to your controller.

downhand
  • 395
  • 1
  • 9
  • 23
0

you can use ng-repeat-start and ng-repeat-end with div as td's wrapper (as an hack)

 <td class="researched" colspan="2" 
     ng-repeat-start="tech in userCtrl.chosenTechs1" 
 title="{{tech.name}}">{{tech.name}}</td>
   <div ng-repeat-end> 
    <td class="available" colspan="2" title="6 trade needed to learn level 1 tech" 
     ng-repeat="5 > $index">6</td>
 </div>
A.B
  • 20,110
  • 3
  • 37
  • 71