通过表单

时间:2016-01-02 18:35:24

标签: angularjs spring-mvc restangular

我的后端控制器看起来像这样

@RequestMapping(value ="/search",method=RequestMethod.POST)
@ResponseBody
public List<Book> searchSubmit(@ModelAttribute Book book, Model model) {

我怎么能得到典型的帖子 - &gt;使用Restangular或Angularjs的响应周期?下面的工作是为了获取所有书籍,但没有使用Book Model参数定位“/ search”映射,我把它放在ng-Grid中。

app.controller('searchController', function($scope, $http, $location, Restangular){
$scope.bookData = {};


$scope.processUpdateForm = function(){


  //data from the form works !!
  var bAuthor = $scope.bookData.author;
  console.log(">>>>>bData"+bAuthor);

  Restangular.all('books').getList().then(function(result) {
      $scope.books = result;
      console.log(">>>>>books: "+angular.toJson($scope.books, 2));
      bookData = angular.toJson($scope.books, 2);
      $scope.$emit('bookData', $scope.books);
  });              

  }
});

但你怎么能真正做一些有用的事情,比如paramaters的查询? 这样做有什么例子吗?

参考文档只显示简单获取全部或获得一个...

我知道你可以做..

Restangular.get('books').get({filters: {
  author: bAuthor,
  genre: bGenre
}});

会发送类似的东西。

 ?filters={"author":"thedata","genre":"thedata"}

但那不行......

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,这就是解决方案......

使用angularjs和restangular lib(也是reqs lodash lib)..

var app = angular.module('myApp', [ 'ngGrid', 'restangular' ]);

var bookData;
app.controller('gridCtrl', [ '$scope', function($scope) {
$scope.myData = [];

$scope.gridOptions = {
    data : 'myData',
    enableColumnResize : true
};

$scope.$on('bookData', function(event, bookData) {
    $scope.myData = bookData.plain();
    console.log("$scope.myData = " + $scope.myData);
    $scope.gridOptions = {
        data : 'myData',
        enableColumnResize : true
    };

    });

} ]);

app.controller('searchController', function($scope, $http, $location,
    Restangular) {

$scope.bookData = {};

$scope.processUpdateForm = function() {
    // data from the form 
    var aData = $scope.bookData.author;
    var gData = $scope.bookData.genre;
    var pData = $scope.bookData.pages;
    var yData = $scope.bookData.year;
    var rData = $scope.bookData.rating;
    Restangular.all(
            'books/rest/' + aData + '/' + gData + '/' + pData + '/' + yData
                    + '/' + rData).getList().then(
            function(result) {
                $scope.books = result;
                $scope.$emit('bookData', result);
                // reset the form..
                $scope.bookData = {};
            });

}
});

在Spring后端检索Params ......

@RequestMapping(value = "/{author}/{genre}/{pages}/{year}/{rating}",method = RequestMethod.GET)
@ResponseBody
public List<Book> searchBooks(@PathVariable String author,
                              @PathVariable String genre,
                              @PathVariable String pages,
                              @PathVariable String year,
                              @PathVariable String rating) {
 Etc....

希望这有助于某人!