2017-04-04 87 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

這是編輯問題時的錯誤,代碼中的名稱相同 –