Jake Jake - 3 years ago 161
reST (reStructuredText) Question

Angularjs: View not updating list after POST

I am currently working on a small angularjs app which is basically a user profile management app.

The problem i am having is with adding users dynamically. When i enter the user data, it successfully POST's to my local server i have setup, BUT i have to refresh the page to see the new user in the users list

I obviously dont want to have to refresh.

-Yes i've tried $scope.apply() after running the POST function

Something i am noticing with Angular Batarang (Debugging tool), is that the scope is updating fine, but there is a blank spot or 'null' value where the new user should be.

Here are the Controllers:

UsersApp.controller('UserListController', [ '$scope', 'userService', function($scope, userService) {


$scope.usersList = userService.usersList;

$scope.users = userService.users;
$scope.user = userService.user;

}]);

UsersApp.controller('AddUserController', function($scope, $window, dataResources, userService) {

$scope.addNew = function addNew(newUser) {
$scope.usersList = userService.usersList;

var firstName = newUser.firstName;
var lastName = newUser.lastName;
var phone = newUser.phone;
var email = newUser.email;

$scope.newUserData = {
firstName , lastName, phone , email
}

new dataResources.create($scope.newUserData);
$scope.usersList.push(dataResources);
$scope.$apply();

};


And Here are my views:

Add User:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="js/minimize.js"></script>

<div ng-controller="AddUserController">

<div class="userInfo" id="usernameDiv">
<h2 id="username">User<img id="showhide" src="images/plus.png" style="position:absolute; padding-left:15px; width:31px; color:white;"></h2>
</div>

<div class="userInfo">
<div id="listInfo">

<form ng-controller="AddUserController">
<input type="text" placeholder= "First Name" ng-model="newUser.firstName"></input>
<input type="text" placeholder= "Last Name" ng-model="newUser.lastName"></input>
<input type="text" placeholder= "Phone Number" ng-model="newUser.phone"></input>
<input type="text" placeholder= "Email" ng-model="newUser.email"></input>
<button type="submit" ng-click="addNew(newUser)">Add User</button>
</form>

</div>
</div>




Users List:

<!DOCTYPE html>
<html>
<head></head>

<body id="">

<div ng-controller="UserListController">
<div class="userInfo">

<h2>List of Users</h2>

<div id="listInfo">

<ul style="list-style-type: none;">
<li ng-repeat="user in usersList">
<!--<p class="userData">ID: {{ user }}</p> -->
<p class="userData"><a style="cursor:pointer;" ui-sref="UserProfile">{{ user.firstName }}</a></p>


</li>
</ul>

</div>

</div>







Factory and Service:

UsersApp.factory('dataResources', [ '$resource', function($resource) {

return $resource('http://localhost:24149/users/:id', {}, {
query: {method:'GET', params:{idnum: '@id'}, isArray:true},
create: {method:'POST', headers: { 'Content-Type': 'application/json' }},
update: {method:'PUT', params:{idnum: '@id'}},
remove: {method:'DELETE', params:{idnum:'@id'}, isArray:true}
});

}]);


UsersApp.service('userService', function(dataResources) {

return {
usersList: dataResources.query()
}

});

Answer Source

I'm not sure if I follow exactly, but I believe you need to deal with a promise from your POST and then push the result. e.g.,

  dataResources.create($scope.newUserData).$promise.then(function(data) {
       $scope.usersList.push(data);
   });

Your service will return a promise and then when the POST is complete your service should return the new user and you just add it to your current list.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download