I have an API that I'm trying to get data from from a separate AngularJS application.
There is a button that I'm using to load in data from the API. When pressed it calls the $scope.getApiData
function, which is bound to a variable called $scope.productlist
. Each entry has a delete button which calls the $scope.deleteProduct
function.
The API is working properly, though it's using ASP.NET 5.
The code called when 'Update' is pressed is also called after a 'Delete' press.
(function () {
'use strict';
angular
.module('app')
.controller('Products', main);
main.$inject = ['$scope', '$http'];
function main($scope, $http) {
$scope.productlist = [];
// Get products from API
$scope.getApiData = function () {
$http({
method: 'GET',
url: "http://localhost:65040/api/products",
}).then(function successCallback(response) {
$scope.productlist = response.data;
}, function errorCallback(response) {
alert('Error');
});
};
// Delete a product
$scope.deleteProduct = function (idx) {
$http({
method: 'DELETE',
url: "http://localhost:65040/api/products",
params: { 'id': idx },
}).then(function successCallback(response) {
$scope.getApiData();
});
};
}
})();
However, when the $scope.getApiData
is called a second time--either manually or by deleting an item--it doesn't correctly update the list to reflect the state of the API, even though the correct results are returned in the HTTP response. The list remains the same, and the whole application has to be restarted for it to reflect the data in the API.
HTML:
<table>
<tr>
<td>ID</td>
<td>Item Name</td>
<td>Price</td>
</tr>
<tr ng-repeat="product in productlist">
<td>{{product.Id}}</td>
<td>{{product.Name}}</td>
<td>{{product.Price}}</td>
<td><button ng-click="deleteProduct(product.Id)">X</button></td>
</tr>
</table>
<button ng-click="getApiData()">Update</button>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="app/app.module.js"></script>
<script src="app/main.js"></script>
Having a problems figuring this out. Thanks for reading!