[英]AngularJS Spring mvc @RequestParam
我是Angularjs的新手,以下Spring控制器通過id從數據庫獲取對象我想使用AngularJs通過@RequestParam獲取此id,我嘗試以下代碼,但收到此錯誤“錯誤:id未定義.findOne @本地主機: 8080 / myapp / scripts / services.js:126:79 $ scope.findOne @ localhost:8080 / myapp / scripts / controllers.js:280:4
Spring MVC控制器
@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服務
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控制器
myappApp.controller('ChartConfigController', function ($scope, ChartConfigService) {
$scope.findOne= function() {
ChartConfigService.findOne($scope.id).then(function(obj) {
$scope.message=obj;
console.log(obj.type);
});
};
});
HTML頁面
<input ng-model="id" ng-change="findOne()" required>
您忘記將id傳遞給findOne作為函數參數:
findOne: function(id) {
var promise = $http.get('app/rest/chartConfigs/getById',{params: {'id': id}}).
then(function (response) {
return response.data;
});
return promise;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.