I am trying to have one very simple CRUD app. It involves one form with single text box. and all the entries posted through box shall display in a grid below text box.
All is well but somehow grid doesn't update with new entry unless page is refreshed. I am using loopback api running on localhost:3000 and my angular app on localhost:9000. Database is MySQL.
Same code works as expected if I am using MEAN stack. Now we have to support mySQL and decoupled API from my application. This is controller.
angular.module('dashboardApp')
.controller('StateCtrl', function ($scope, $http) {
$scope.formData = {};
$scope.baseUrl = '//localhost:3000/api/v1/states';
$http.get($scope.baseUrl).success(function(states) {
$scope.states = states;
});
$scope.create = function() {
$http.post($scope.baseUrl, $scope.formData)
.success(function(states) {
$scope.states = states;
})
.error(function(states) {
console.log('Error: ' + states);
});
};
});
This is view.
<form class="form-inline" role="form" data-ng-submit="create()">
<div class="form-group">
<label class="sr-only" for="name">State</label>
<input type="text" class="form-control" id="name" placeholder="Enter state" ng-model="formData.name">
</div>
<button type="submit" class="btn btn-default">Enter</button>
</form>
<table class="table table-striped">
<tr ng-repeat="state in states">
<td>{{state.name}}</td>
</tr>
</table>
Any help is appreciated. just a note: I've tried using services/resources as well instead of $http.
console.log(states)and see what is logged right out of you submit the formconsole.log("states " + JSON.stringify(states));returnsstates {"name":"new entry","id":27}