1

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>

1 Answer 1

1

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.