AngularJS Spring mvc @RequestParam

AngularJS Spring mvc @RequestParam

我是 Angularjs 的新手,下面的 Spring 控制器通过我想要的 id 从数据库中获取对象 使用 AngularJs 通过 @RequestParam 获取此 ID,我尝试以下代码但出现此错误 “错误:未定义 id .findOne@ localhost: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;
    }