I'm building a web app using Yeoman Angular-fullstack. I'm confused about how to update my $scope variables so that once a change is made, the results are automatically displayed on the html page without a refresh.
HTML
<h1 class="page-header">Drugs:</h1>
<div class="row">
<div class="col-sm-4 col-sm-offset-4">
<div ng-repeat="current in alldrugs">
<p>
<a href="/edit={{current._id}}"> <button class="btn btn-default" type="button">{{current.name}}</button></a>
<a ng-click="delete(current)"><button class="btn btn-default" type="button">delete</button></a>
</p>
</div>
</div>
</div>
Controller.js
'use strict';
angular.module('firstApp')
.controller('MainCtrl', function ($scope, $http) {
$scope.alldrugs = [];
$http.get('/api/drugs').success(function(alldrugs) {
$scope.alldrugs = alldrugs;
});
$scope.delete = function(thing) {
$http.delete('/api/drugs/' + thing._id);
$http.get('/api/drugs').success(function(alldrugs) {
$scope.alldrugs = alldrugs;
});
};
});
When $scope.delete is called, the item is deleted, however the page does not reflect the changes until I refresh the page. I'm thinking it has to do with the scope of the http callback function. Any help would be appreciated, thanks.
splice
but your backend should be updating the list. not sure if there is error on backend. try chaining yourdelete
andget
– maddog May 11 at 23:56