As a newcomer to AngularJS, I have a Spring controller that retrieves an object from the database based on its id. I want to pass this id using @RequestParam in AngularJS. However, when attempting to do so, I encountered the following error message: "Error: id is not defined". Here is the code snippet that resulted in the error:
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>