Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing DOM object using AngularJS

I have the following code as Spring Controller:

@RequestMapping(value = "/city/{city}")
    public ModelAndView cityRestaurants(@PathVariable String city){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("restaurant");
    List<Restaurant> list = null;
    try {
        list = rService.getRestaurantsFromCity(city);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mav.addObject("list", list);

    return mav;
    }

How can I access "list" on client side using AngularJS?

like image 991
Wuzyn Avatar asked Dec 19 '22 18:12

Wuzyn


1 Answers

It's possible to define a controller that does not have a view but renders the return parameters as JSON. In order for this to work the Jersey library needs to be on the classpath:

@Responsebody
@RequestMapping(method=RequestMethod.GET, value = "/city/{city}", produces = "application/json" )
public List<Restaurant> cityRestaurants(@PathVariable String city){
        return rService.getRestaurantsFromCity(city);
 }

You can already test this service with something like Postman and see that it's working, then on the client side we can call this service using an Ajax request:

 $http({
        method: 'GET',
        url: 'https://www.example.com/city/yourcityId',
     }).success(function(data){
        alert('restaurants: ' + data);
    }).error(function(){
        alert("error");
    });

This should print the cities restaurants.

like image 134
Angular University Avatar answered Jan 02 '23 15:01

Angular University