简体   繁体   English

AngularJS Spring mvc @RequestParam

[英]AngularJS Spring mvc @RequestParam

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 我是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 Controller 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 Service 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 Controller 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 page HTML页面

 <input ng-model="id" ng-change="findOne()" required>

You forgot to pass id to findOne as a function parameter: 您忘记将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM