Accessing a DOM Object Using AngularJS Function

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 the client side list using AngularJS?

+4
source share
3 answers

As suggested by @jhadesdev instead of using the Model and View approach, you can use the Restful Web service, which returns data in angular js Controller in JSON format. There you can change the $ scope variable as desired depending on the result. For a detailed guide, you can go to the spring website.

1 Creating a RESTful Web Service

2 Using a RESTful Web Service Using AngularJS

+2

, , JSON. , , :

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

Postman , , Ajax:

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

.

+8

AngularJs REST API. JSon, AngularJs Javascript .

, RESTAngular:

var app = angular.module('MyApp', ['restangular'])

.config(function(RestangularProvider) {
    RestangularProvider.setBaseUrl('my.api/rest/');
});

app.controller('MyCtrl', function($scope, Restangular) {
    $scope.city = 'Chicago';
    $scope.restaurants = [{name:'Restaurant 1'}, {name:'Restaurant 2'}];

    // Defines a resource located at "my.api/rest/restaurants/Chicago"
    var source = Restangular.all('restaurants').one($scope.city);

    source.getList().then(function(result) {
        $scope.restaurants = result;
    });
});

Spring MVC JSon. , , .

+1
source

Source: https://habr.com/ru/post/1523689/


All Articles