I doing a post request to a rest api then returning the data I send together with an id then add it to the table but the table is not updating.
$scope.addCategory = function(category){
$http.post('http://localhost/myappi/API/index.php/Api/categories',category).
success(function(data)
{
console.log('Data returned: ', data);
$scope.categories.push(data); //add it to the table ?
})
.error(function(err) {
console.log(err);
})
};
And this is my table.
<table class="table table-striped">
<tr>
<th>Name</th><th>Action</th>
</tr>
<tr ng-repeat="category in categories">
<td>{{ category.name }}</td><td>
<button class="btn btn-warning" ng-click="editCategory(category)">Edit</button>
<button class="btn btn-danger" ng-click="deleteCategory(category.id)">Delete</button></td>
</tr>
</table>
To populate the table when the page loads I have an init function which returns a list of categories. I could change this method so it triggers whenever the user tampers with the data but it doesn't seem efficient to get all the data from the server when I edit a field for example. Also when I delete a category the data is removed correctly.
// How the table is populated on page load.
$scope.init = function() {
$http.get('http://localhost/myappi/API/index.php/Api/categories/').
success(function(data) {
$scope.categories = data;
console.log(data);
})
.error(function(err) {
console.log(err);
})
};
How can I close this question ? I had $scope.categories=[]; at the front of the controller.
$scope.categoriesand what isdatain response?$scope.categories.push(category);