1

I have created a simple app with Spring and AngularJS with CRUD functions.

Now, my scope variable that contains the arraylist that I pass is not updating when I create a new one.

This is my code for my AngularJS controller

$scope.agency = {id: null, name: '', contact: '', employees: []};
$scope.agencies = [];

$scope.getAllAgencies = function() {
    AgencyService.getAllAgencies()
        .then(
            function (response) {
                $scope.agencies = response;
            }
        )
};

$scope.getAllAgencies();

$scope.createAgency = function(agency) {
    AgencyService.createAgency(agency)
        .then(
                $scope.getAllAgencies(),
            function(errReponse){
                console.error('Error while creating Agency.');
            }
        );
};

$scope.submit = function () {
    console.log("Saving new Agency");
    $scope.createAgency($scope.agency);

    $scope.reset();
};

$scope.reset = function(){
    $scope.agency = {id: null, name: '', contact: '', employees: null};
    $scope.myForm.$setPristine(); //reset Form
};

Service

App.factory('AgencyService', ['$http', '$q', function($http, $q) {
return {
    getAllAgencies: function() {
        return $http.get('/agency')
            .then(
                function(response) {
                    return response.data;
                },
                function(errResponse){
                    console.error('Error while fetching agencies');
                    return $q.reject(errResponse);
                }
            )
    },
    createAgency: function(agency) {
        console.log("Creating Agency");
        return $http.post('/agency', agency)
            .then(
                function (response) {
                    return response.data;
                },
                function (errResponse) {
                    console.error('Error while create agency');
                    return $q.reject(errResponse);
                }
            )
    }
};

}]);

And these are my methods in Spring

Get Agencies

@RequestMapping(value = "/agency", method = RequestMethod.GET)
public ResponseEntity<List<Agency>> getAllAgencies() {
    List<Agency> agencies = agencyService.getAllAgencies();
    if (agencies.isEmpty()) {
        return new ResponseEntity<List<Agency>>(HttpStatus.NO_CONTENT);
    }
    return new ResponseEntity<List<Agency>>(agencies, HttpStatus.OK);
}

Creation of Agency

@RequestMapping(value = "/agency", method = RequestMethod.POST)
public ResponseEntity<Void> createAgency(@RequestBody Agency agency, UriComponentsBuilder ucBuilder) {

    if(agencyService.isAgencyExists(agency)) {
        System.out.println("CONFLICT");
        return new ResponseEntity<Void>(HttpStatus.CONFLICT);
    }

    agencyService.saveAgency(agency);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(ucBuilder.path("/agency/{id}").buildAndExpand(agency.getId()).toUri());
    return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}

Note: that when I debug, after the post method it does not continue to my get method.

There is no error in this, when I reload it gets all including the one I just created.

Thanks!

7
  • Is it like when you create a new agency, the list is not getting updated? Commented Apr 27, 2016 at 6:21
  • Exactly! That's my problem. If you see my code, that should be working. Commented Apr 27, 2016 at 6:21
  • Do you really have to call getAllAgencies after createAgency()? Can you not just push the saved agency into the existing array after a 200 OK response? Commented Apr 27, 2016 at 6:25
  • I tried that and it worked, but I wanted to call getAllAgencies after the createAgency. I was doing things like this before, I don't know why it's not working now. This is my reference websystique.com/springmvc/spring-mvc-4-angularjs-example. p.s I'm considering real time calls so this might be the best way. Commented Apr 27, 2016 at 6:28
  • Which browser are you using? Is it IE? Commented Apr 27, 2016 at 6:46

1 Answer 1

4

I think the create agency function call should be like below:

$scope.createAgency = function(agency) {
    AgencyService.createAgency(agency)
        .then(
                $scope.getAllAgencies,
            function(errReponse){
                console.error('Error while creating Agency.');
            }
        );
};

first parameter in then should be the function "$scope.getAllAgencies" instead you are passing the value the function returned "$scope.getAllAgencies()".

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

2 Comments

It worked, I missed this in the tutorial. Thanks bro! I just want to ask, which is better to use? $http or $resource?
I think this should answer your question link

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.