0

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

});
0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man, it worked! Wasn't sure if promise was what i was looking for or not, but it updates nicely now without error
1

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

1 Comment

This did work without having to make a promise, which is a huge help. Thanks for that! As for my controller, i forgot to refactor that unnecessary leftover object thanks for the heads up
0

Same idea for angular2 using observables.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.