I am binding a scope variable, an array to ng-repeat div tag (a table basically). When I dynamically add any data to the array, it works! A row is added to the table.
But when I remove an element from the array, the change doesn't reflect on the table. One row should be removed.
Following is the code that I'm working with (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: Please see below!
Weird thing is, In console log, it says that changes are made as expected to $scope variable, only view(table) is not getting updated. And if I don't put "track by $index", add and remove both stops reflecting in table!
Any help appreciated. Thanks!
EDIT 2: The code you have asked for:
<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>