Angularjs calling java functions
So when using GET method usual we fetch some data, if we want to send some data to server (ex. an id for person to be deleted) we use POST method or DELETE method, in my example I'll use POST method for simplification. Angular and java communicate thru RESTFUL services (JAX-RS), You cant call java function in angular js or vice verse. I'll show simple example of fetching data and sending data (fetch all persons, delete person with given id).
Here is an example where You can start learning from:
Java Person Controller
@Controller@RequestMapping(value = "/person")public class PersonController{ private final PersonService personService; @Autowired public PersonController(final PersonService personService) { this.personService= personService; } @RequestMapping(value = "/", method = { RequestMethod.GET }) @ResponseBody public List<Person> getPersons() { return personService.getPersons(); } @RequestMapping(value = "/delete/{personId}", method = { RequestMethod.POST}) @ResponseBody public HttpStatus deletePerson(@PathVariable final long personId) { return personService.deletePerson(personId); }}
Java Person Service
public class PersonService{ private final PersonRepository personRepository; @Autowired public PersonService(final PersonRepository personRepository) { this.personRepository= personRepository; } public List<Person> getPersons() { return personRepository.findAll();; } public HttpStatus deletePerson(final long id) { if (id > 0) { personRepository.delete(id); return HttpStatus.OK; } else { return HttpStatus.INTERNAL_SERVER_ERROR; } }}
Java Person Repository
public interface PersonRepository{ public void delete(int personId); public List<Person> findAll();}
Angular app.js
(function() { var app = angular.module('personApp',[]); app.controller('personCtrl',function($scope,$http){ $scope.getPersons = function(){ $http.get('person/').success(function(response){ $scope.allPersons = response.data; }).error(function(){ //handle error }) }; $scope.deletePerson = function(personId){ $http.delete('person/'+personId).success(function(response){ $scope.deleteResponseStatus = response.status; }).error(function(){ //handle error }) } })})();
Html
<html ng-app="personApp"> <body ng-controller=""personCtr> <input type="submit" ng-click="getPersons()"/> <input type="submit" ng-click="deletePerson(somePersonIdFromTableOrSomething)" </body></html>
Hope it will help, not tested but generally that is the flow..send request to controller with person id, find it in database and delete it
You need to attack the issues individually. Try testing your rest service alone. Use POSTMAN or any other REST Client (https://addons.mozilla.org/en-US/firefox/addon/restclient/) and test if the service is behaving accordingly. Only when that's successful, check on the Javascript client (AngularJS code) and see if the request (headers, params and body) is the same as the one generated by the REST client.
you can call java method or service in angularjs controller this way:
app.controller('personCtrl',['$scope', '$http', function($scope, $http){ $scope.firstName = "John"; $scope.personList = []; $scope.typelistload = function() { $http.get( 'http://localhost:8080/SpringMVCHibernate/Person/getPersonList' ).success(function(data) { $scope.personList = data; }).error(function() { ngToast.danger( "Some problem in Listing:"); }); };}]);