【问题标题】:ng-if not working when nested table iteration using ng-repeat使用 ng-repeat 进行嵌套表迭代时 ng-if 不起作用
【发布时间】:2017-11-18 19:44:30
【问题描述】:

当表格内的表格在那个时候动态重复时 ng-if 不起作用。当我在子表格中添加一行时采取错误的父索引。我像这样使用但在输入字段中工作正常但按钮不起作用当 ng-if 条件采用错误的父索引时

例如:

<div  id="parentdivId" ng-repeat="parent in listOfParentTables">

            <table >
            <thead class="evlhead ingpopuphead">
              <tr>
                <th >A</th>
                <th >B</th>               
                <th >C</th> 
              </tr>
            </thead> 

            <tbody >
             <tr  ng-repeat="child in parent.childMaster" ng-if="child.status!=2">  
             <td > 
              <select   id="a10" ng-model="child.a10">
               <option ng-repeat="list in listMaster" value={{list.ID}}>{{list.NAME}}</option>
              </select>  
              </td> 
             <td ><div class="addrembtn" ><input class="btnadd" type="button" value="+" id="addbutton10"  ng-click="addNewChildRow($parent.$index,$index)"><input class="btnrem" type="button" value="-" id="deletebutton10"  ng-click="removechildRow($parent.$index,$index)"></div></td>
            </tr>
               </tbody> 
           </table>


           <div style="float:left;"> 
            <button class="parentbtn" style="margin:37px 0px 0px 17px !important;width: 75px;" value="AddTable" id="basebutton" ng-click="addNewParenttable($index)">+ &nbsp;Gasto</button>
          </div>
          <div style="float:left;"> 
            <button class="parentbtn" style="margin: 12px 0px 0px 17px !important;width: 75px;" value="AddTable" id="basebutton" ng-click="removeParenttable($index)">- &nbsp;Eliminar</button>
          </div>

           </div>

【问题讨论】:

  • 用 ng repeat 显示完整的表格
  • 请参阅我在stackoverflow.com/questions/20111636/… 上的回答。它可能会帮助你
  • 在上面的代码中,当 ng-if 条件被移除时它工作正常,但我想放那个条件

标签: angularjs


【解决方案1】:

在嵌套的 ng-repeat 中,$index 不会给出被迭代数组中元素的索引。使用类似的东西,

ng-click="addNewChildRow(listOfParentTables.indexOf(parent),parent.childMaster.indexOf(child))"
ng-click="addNewParenttable(listOfParentTables.indexOf(parent))"

indexOf 总是在 ng-repeat 中返回原始索引 Plunker

希望对你有帮助!

【讨论】:

  • 感谢重播。我试试这个。
  • 非常感谢 :) 乐于助人
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
  • 1970-01-01
相关资源
最近更新 更多