My AngularJS app displays data in a table using ng-repeat
. AngularJS makes a GET request, which retrieves an array. The table correctly displays the elements of the array.
The page contains a button that calls a function to modify the array. However, the table is not updated to reflect the modified array.
I have tried the following:
- Calling
$scope.apply()
after the update. - Enclosing the update within a function passed to
$scope.apply()
as in$scope.apply(function...)
. Link - Using
$timeout
Link
None of these attempts worked. Additionally, I believe that ng-repeat
calls apply()
itself.
AngularJS
<script>
var app2 = angular.module('myApp2', []);
app2.controller('StocksController', function ($scope, $http, $timeout) {
// When the page loads...
// Load myData with REST GET
$http.get("http://example.com/exWebAPI/api/Ticker/").then(function (response) {
$scope.myData = response.data;
});
// Update the array
$scope.SendData = function () {
alert('Hello from SendData.');
$scope.myData.splice(0, 2);
$scope.$apply();
};
});
</script>
HTML
<div ng-app="myApp2" ng-controller="StocksController">
<h2>Reportable Tickers</h2>
<p>
<a href="#" ng-click='AddTicker()'>Add Ticker</a>
</p>
<table class="table">
<tr>
<th>
Symbol
</th>
<th></th>
</tr>
<tr ng-repeat="x in myData">
<td>
{{ x.Ticker }}
</td>
<td>
<a href="#" ng-click='EditTicker(x.Ticker)'>Edit</a> |
<a href="#" ng-click='DetailsTicker(x.Ticker)'>Details</a> |
<a href="#" ng-click='DeleteTicker(x.Ticker)'>Delete</a>
</td>
</tr>
</table>
<hr />
<div>
<div ng-controller="StocksController">
<button ng-click="SendData()">Submit</button>
<button ng-click="QueryData()">Query Data</button>
<hr />
</div>
</div>
</div>