Reputation: 95
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>
Upvotes: 1
Views: 6546
Reputation: 7998
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;
}
Upvotes: 1