2017-04-04 89 views
0

我绑定一个范围变量,一个数组ng-repeat div标记(基本上是一个表)。 当我动态地将任何数据添加到数组中时,它的工作原理!一行被添加到表中。AngularJS范围变量(数组)更改不反映在ng-repeat

但是,当我从数组中删除一个元素,更改不反映在表上。应该删除一行。

以下是我与(JavaScript)的工作代码:

$scope.myfields = []; 
    $scope.addField = function() { 
     $scope.myfields.push({ "name": "", "type": "", "required": "", "enum": "" }); 
     console.log("add: " + $scope.myfields.length); 
     console.log(JSON.stringify($scope.myfields)); 
    } 

    $scope.removeField = function (index) { 
     $scope.myfields.splice(index, 1); 
     console.log("remove: " + $scope.myfields.length); 
     console.log(JSON.stringify($scope.myfields)); 
    } 

EJS:请参考下面!

奇怪的是, 在控制台日志中,它表示对预期的$ scope变量进行了更改,只有view(table)没有更新。 如果我不把“通过$ index追踪”,添加并删除反映在表中的两个站!

任何帮助表示赞赏。谢谢!

编辑2: 您所要求的代码:

<div class="col-md-12"> 
        <p style="text-align:center"><strong>DEFINE CUSTOM FIELDS:</strong></p> 
        <br> 
        <div style="text-align:center"> 
         Click on '+' button to add custom field: 
         <div class="fa fa-plus-circle" ng-click='addField()'> </div> 
         <div class="fa fa-minus-circle" ng-click='removeField(0)'> </div> 
        </div> 
        <br> 
        <div data-responsive-table> 
         <table data-table> 
          <thead > 
          <tr > 
           <th data-event='sort'> 
           Field Name 
           </th> 
           <th data-event='sort'> 
           Type 
           </th> 
           <th data-event='sort'> 
           Is Required? 
           </th> 
           <th data-event='sort'> 
           Enumeration 
           </th> 
          </tr> 
          </thead> 
          <tbody > 
          <tr data-parent-row ng-repeat="um in dynamicFields track by $index"> 
           <td> 
            <input placeholder="Name" ng-model="um.name" validation="required" > 
           </td> 
           <td> 
            <select style='height: 45px;' ng-model="um.type" > 
             <option value="string">string</option> 
             <option value="boolean">boolean</option> 
             <option value="integer">integer</option> 
            </select> 
           </td> 
           <td> 
            <select style='height: 45px;' ng-model="um.required" > 
             <option value="true">true</option> 
             <option value="false">false</option> 
            </select> 
           </td> 
           <td> 
            <input placeholder="Enum" ng-model="um.enum" validation="required" > 
           </td> 
          </tr> 
          </tbody> 
         </table> 
        </div> 
       </div> 
+0

发布代码,其中您调用removeField()和addField(),并在其中定义dyna micFields array – Karim

+0

如何通过ng-click触发你的removeField函数? –

+0

你的代码看起来不错。显示你的调用removeField的代码。 –

回答

1

在NG-重复的变量名称应该是myfields而不是dynamicfields

因为在你的控制器是$scope.myfields,在你的查看它应该是

ng-repeat="um in myfields track by $index" 
+0

这是编辑问题时的错误,代码中的名称相同 –