简体   繁体   中英

AngularJS Spring mvc @RequestParam

I'm newbie in Angularjs,The following Spring controller get object from database by id I want to get This id by @RequestParam using AngularJs ,I try the following code but get this error "Error: id is not defined .findOne@ localhost:8080/myapp/scripts/services.js:126:79 $scope.findOne@ localhost:8080/myapp/scripts/controllers.js:280:4

Spring MVC Controller

    @RequestMapping(value = "/rest/chartConfigs/getById",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RolesAllowed(AuthoritiesConstants.ADMIN)
    public ChartConfigs findOne(@RequestParam(value = "id") Integer id) {
     System.out.println(chartConfigService.getOne(id));
         return chartConfigService.getOne(id);

    }

AngularJS Service

    myappApp.factory('ChartConfigService', function ($http) {
    return {
    findOne: function() {
        var promise = $http.get('app/rest/chartConfigs/getById',{params: {id: id}}).
                        then(function  (response) {
            return response.data;
        });
        return promise;
    }
  }
 });

AngularJS Controller

  myappApp.controller('ChartConfigController', function ($scope, ChartConfigService) {
    $scope.findOne= function() {
     ChartConfigService.findOne($scope.id).then(function(obj) {
            $scope.message=obj;
            console.log(obj.type);
        });
    };      
  });

Html page

 <input ng-model="id" ng-change="findOne()" required>

You forgot to pass id to findOne as a function parameter:

findOne: function(id) {
        var promise = $http.get('app/rest/chartConfigs/getById',{params: {'id': id}}).
                        then(function  (response) {
            return response.data;
        });
        return promise;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM