1

I was trying to display items in a list with Angular js. I hope to organize the items with 3 items in a row with the following code:

  <div class='row-fluid' ng-repeat="region in regions" ng-show="$index % 3 == 0">

      <span  class='span4 checkbox'>
        <input type="checkbox"  ng-model="regions[$index].checked" ng-hide='regions[$index].text == null'> {{regions[$index].text}}
      </span>
      <span  class='span4 checkbox'>
        <input type="checkbox"  ng-model="regions[$index+1].checked" ng-hide='regions[$index+1].text == null'> {{regions[$index+1].text}}
      </span>
      <span  class='span4 checkbox'>
        <input type="checkbox"  ng-model="regions[$index+2].checked" ng-hide='regions[$index+2].text == null'> {{regions[$index+2].text}}
      </span>       
  </div>

If works fine when I use "ng-show". But when I switch it with 'ng-if', it doesn't work at all. (it shows all the 'regions' and does not filter with $index % 3 == 0). I was thinking "ng-if" should be a more standard way to implement my layout design but have no idea why it does not work at all.

Following is the code that won't work:

  <div class='row-fluid' ng-repeat="region in regions" ng-if="$index % 3 == 0">

      <span  class='span4 checkbox'>
        <input type="checkbox"  ng-model="regions[$index].checked" ng-hide='regions[$index].text == null'> {{regions[$index].text}}
      </span>
      <span  class='span4 checkbox'>
        <input type="checkbox"  ng-model="regions[$index+1].checked" ng-hide='regions[$index+1].text == null'> {{regions[$index+1].text}}
      </span>
      <span  class='span4 checkbox'>
        <input type="checkbox"  ng-model="regions[$index+2].checked" ng-hide='regions[$index+2].text == null'> {{regions[$index+2].text}}
      </span>       
  </div>

Could you please help me with that?

JDD
  • 51
  • 7
  • 3
    Please show the code that *doesn't* work. It's not useful to see the working code. – JJJ Jul 08 '15 at 18:32
  • Where is not working code? Apart from your issue stated above, its worth to check the difference between ng-if and ng-show/hide. There is a strong reason what to opt. http://stackoverflow.com/questions/19177732/what-is-the-difference-between-ng-if-and-ng-show-ng-hide – TheKingPinMirza Jul 08 '15 at 18:38
  • @Juhana, I added the not-working code. The only difference is whether I used ng-if or ng-show – JDD Jul 08 '15 at 18:43
  • Did you try with ng-if="$index % 3 == '0'" – TheKingPinMirza Jul 08 '15 at 18:45
  • 2
    `ng-if` stops the html from making it to the DOM - if the HTML isn't there your `$index` wont be incremented how you think it will (at least that's what I *think* - explains the difference with `ngShow` to `ngIf` – tymeJV Jul 08 '15 at 18:50
  • Can you provide a jsfiddle ? – Gonzalo.- Jul 08 '15 at 19:19
  • @immirza, yes, I tried, and it does not work either. – JDD Jul 08 '15 at 21:56
  • Which version you are using of angular js? – Shalu Singhal May 11 '16 at 05:29

1 Answers1

0

Someone who is trying to find answer now...try having $index (or any variable that you have problem using with ng-if) declared into your controller first while using it with ng-if. Without that ng-if may be creating its own scope. For more info visit this question on stackoverflow.

  1. Why ng-show works with ng-repeat but ng-if doesn't?
  2. ng-if not working, ng-show works
manntsheth
  • 440
  • 6
  • 8