AngularJS Spring mvc @RequestParam [英] AngularJS Spring mvc @RequestParam

查看:110
本文介绍了AngularJS Spring mvc @RequestParam的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angularjs的新手,以下Spring控制器通过ID从数据库获取对象,我想使用AngularJs通过@RequestParam获取此ID,我尝试以下代码,但得到此错误错误:未定义ID.findOne @本地主机:8080/myapp/scripts/services.js:126:79$ scope.findOne @本地主机:8080/myapp/scripts/controllers.js:280:4

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控制器

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服务

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控制器

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页面

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

推荐答案

您忘记了将ID作为函数参数传递给 findOne :

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;
    }

这篇关于AngularJS Spring mvc @RequestParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆