【问题标题】:AngularJS - Using $resource.query with params objectAngularJS - 使用带有参数对象的 $resource.query
【发布时间】:2013-05-23 07:32:17
【问题描述】:

我正在尝试接听angular.js 并努力找出一些记录较少的事情。

考虑一下 - 我在服务器上有一个搜索方法,它接受查询参数并返回搜索结果集合,并响应 GET /search.json 路由(Rails FWIW)。

因此,使用jQuery,示例查询将如下所示:

$.getJSON('/search', { q: "javascript", limit: 10 }, function(resp) {
  // resp is an array of objects: [ { ... }, { ... }, ... ]
});

我正在尝试使用 angular 来实现这一点,并围绕它是如何工作的。这就是我现在拥有的:

var app = angular.module('searchApp', ['ngResource']);

app.controller('SearchController', ['$scope', '$resource', function($scope, $resource){

  $scope.search = function() {
    var Search = $resource('/search.json');
    Search.query({ q: "javascript", limit: 10 }, function(resp){
      // I expected resp be the same as before, i.e
      // an array of Resource objects: [ { ... }, { ... }, ... ]
    });
  }
}]);

在视图中:

<body ng-app="searchApp">
  ...
  <div ng-controller="SearchController">
    ...
    <form ng-submit="search()">...</form>
    ...
   </div>
</body>

但是,我不断收到TypeError: Object #&lt;Resource&gt; has no method 'push'$apply already in progress 之类的错误。

如果我将 $resource 初始化更改为以下内容,事情似乎会按预期进行:

var Search = $resource("/search.json?" + $.param({ q: "javascript", limit: 10 }));
Search.query(function(resp){ ... });

$resource 初始化一次,然后根据请求搜索的变化传递不同的查询参数似乎更直观。我想知道我是否做错了(很可能)或者只是误解了使用查询参数对象调用$resource.query 作为第一个参数是可行的文档。谢谢。

【问题讨论】:

    标签: angularjs query-parameters angular-resource


    【解决方案1】:

    TypeError: Object # has no method 'push' and $apply already 进行中

    因为您尚未定义名为 Search 的资源。首先,您需要定义这样的资源。 Doc: $resource。这是一个示例实现

    angular.module('MyService', ['ngResource'])
           .factory('MyResource', ['$resource', function($resource){
    
        var MyResource = $resource('/api/:action/:query',{
            query:'@query'
        }, { 
            search: {
                method: 'GET',
                params: {
                    action: "search",
                    query: '@query'
                }
            }
        }); 
        return MyResource;
    }]); 
    

    在你的应用中包含这个模块并在这样的控制器中使用它

    $scope.search_results = MyResource.search({
       query: 'foobar'  
    }, function(result){}); 
    

    但是我不确定这是否是您需要的。资源服务与 RESTful 服务器端数据源(即 REST API)进行交互。

    也许你只需要一个简单的 http get:

     $http({method: 'GET', url: '/someUrl'}).
      success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
      }).
      error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
    

    http://docs.angularjs.org/api/ng.$http

    【讨论】:

    • 事先设置var Search = $resource(...) 不就行了吗?我想我仍在研究角度服务和工厂的想法。此外,我的第一次尝试是使用$http,效果很好。我也想试试$resource,只是为了感受一下使用它们。谢谢!
    • Hm var Search = $resource(...) 实际上是正确的。但是资源期望返回一个对象,但您返回一个数组?尝试在您的查询方法上设置 isArray:true,类似问题groups.google.com/forum/#!topic/angular/MT1vIakNEVM
    • 为什么要覆盖查询参数?
    猜你喜欢
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2016-09-22
    • 2014-05-30
    • 2021-03-02
    相关资源
    最近更新 更多