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!

Aaron
  • 2,591
  • 4
  • 27
  • 45
  • Is it like when you create a new agency, the list is not getting updated? – aksappy Apr 27 '16 at 06:21
  • Exactly! That's my problem. If you see my code, that should be working. – Aaron Apr 27 '16 at 06: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? – aksappy Apr 27 '16 at 06: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 http://websystique.com/springmvc/spring-mvc-4-angularjs-example/. p.s I'm considering real time calls so this might be the best way. – Aaron Apr 27 '16 at 06:28
  • Which browser are you using? Is it IE? – Maverick Apr 27 '16 at 06:46
  • If 'getAllAgencies' is getting invoked but actual rest end point is not, then it may be a case of caching on browser for get requests – Maverick Apr 27 '16 at 06:49
  • @Maverick here's the thing, in createAgency() I call the getAllAgencies(). However when I debug, the first thing that it gets in is at the getAllAgencies() method it will only add afterwards, it's kinda weird. – Aaron Apr 27 '16 at 06:53

1 Answers1

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()".

Maverick
  • 454
  • 2
  • 11
  • It worked, I missed this in the tutorial. Thanks bro! I just want to ask, which is better to use? $http or $resource? – Aaron Apr 27 '16 at 07:07
  • I think this should answer your question [link](http://stackoverflow.com/questions/13181406/angularjs-http-and-resource) – Maverick Apr 27 '16 at 07:25