2015-11-02 39 views
0

我已经使用AngularJS ng-repeat制作了一个表格(请参阅链接how it looks like)。在第二列(每个)中,我试图通过点击上面的相应链接来切换一段数据。它可以工作,但只适用于表格中的奇数行。切换在Angular ng-repeat内无法正常工作

<tr ng-repeat='service in services | filter:searchText' ng-class="{{service.status}} ? 'success' : 'danger'"> 
<td class='status-col'><span class='icon' ng-class="{{service.status}} ? 'glyphicon glyphicon-ok' : 'glyphicon glyphicon-remove'"></span></td> 
<td> 

    <a class='toggle' href="javascript:void(0);">{{service.name}}</a> 


    <div> 
{{service.stateDesc}} 
    </div> 

    <script type="text/javascript"> 
      $(function() // run after page loads 
      { 
      $(".toggle").click(function() 
      { 
       // hides matched elements if shown, shows if hidden 
       $(this).next().toggle(); 


       return false; 
      }); 
      }); 
    </script> 


</td> 

试图重写它通过以下方式,我插入

<script type="text/javascript"> 
      $(function() // run after page loads 
      { 
      $(".link").click(function() 
      { 
       // hides matched elements if shown, shows if hidden 
       if ($(this).next().attr('ng-show')=='false') { 
       $(this).next().attr('ng-show','true'); 
       $(this).next().removeClass("ng-hide"); 
       console.log('true' + $(this).next().html()); 
       //return false; 
       } 

       else if ($(this).next().attr('ng-show')=='true') { 
       $(this).next().attr('ng-show','false'); 
       $(this).next().addClass("ng-hide"); 
       console.log('false'+ $(this).next().html()); 
       //return false; 
       }; 

       //return false; 
      }); 
      }); 
    </script> 

的console.log帮助我明白,当我点击第一个链接(例如),我if-else对第一个元素执行n次(n等于剩余行数,包括初始值)。

就像那个(从我的控制台复制粘贴):

*true 
    WS Payroll 

false 
    WS Payroll 

true 
    WS Payroll 

false 
    WS Payroll 

true 
    WS Payroll 

false 
    WS Payroll 

true 
    WS Payroll 

false 
    WS Payroll* 

因此,的剩余次数完成对“假”(2×N个),它不会打开。但是,当这个链条导致“真实”时,它就会起作用。但无论如何这不是正确的行为。

但是,当我尝试评论整个'else'语句时,脚本被正确执行,每次单击重复项时只执行一次。

有没有人有一个想法如何发生?非常感谢您的帮助。我是新来的角,所以我真的依靠你的帮助。

+0

首先要do就是从你的'ng-repeat'中删除你的'