Jake
Jake

Reputation: 38

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()
}

});

Upvotes: 0

Views: 3016

Answers (3)

Johansrk
Johansrk

Reputation: 5250

Same idea for angular2 using observables.

public posts: any;    
onPost(input) {
        this.dataService.jsonserverPost(input)
        .subscribe(
          (data: any) => {
            this.posts.push(data);        
          }
      );
      }

Upvotes: 0

Michael
Michael

Reputation: 3104

See $resource documentation:

non-GET "class" actions: Resource.action([parameters], postData, [success], [error])

According to the doc your code should look like this:

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

controller: you don't need to make a new userdata object, you can just use newUser

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

  $scope.usersList = userService.usersList;

  $scope.addNew = function addNew(newUser) {
    dataResources.create($scope.newUser,
      function(data) {
        $scope.usersList.push(data);
      }
    );
  };    
};

Upvotes: 1

Arthur Frankel
Arthur Frankel

Reputation: 4705

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.

Upvotes: 2

Related Questions