1

好的,所以我认为我在这里遗漏了一些基本的东西,但我无法通过阅读文档和其他示例来弄清楚。我在工厂有这样的资源:

loteManager.factory('Lotes', function($resource) {
  return $resource('./api/lotes/:id',{ id:"@id" }, {
     get:  {method:'GET', isArray:true}
   });
});

我的控制器:

loteManager.controller('LoteCtrl',
  function InfoCtrl($scope, $routeParams, Lotes) {
    Lotes.get(function (response){
      console.log(response);
    });
});

当我像这样手动定义 id 时它可以工作,$resource('./api/lotes/21'所以我认为问题是将 id 传递给工厂,但我已经尝试添加params:{id:"@id"},但这也不起作用。

4

2 回答 2

2

你需要传入id。

像这样的东西:

loteManager.controller('LoteCtrl',
  function InfoCtrl($scope, $routeParams, Lotes) {
    Lotes.get({id: $routeParams.loteId}, function (response){
      console.log(response);
    });
});

...假设您定义了这样的路线:

$routeProvider.when('/somepath/:loteId, {
    templateUrl: 'sometemplate.html',
    controller: LoteCtrl
});

根据文档

var User = $resource('/user/:userId', {userId:'@id'});
var user = User.get({userId:123}, function() {
  user.abc = true;
  user.$save();
});
于 2013-06-21T20:25:03.637 回答
1

我认为你的问题是你说你的'get'方法(id)有参数,但是当你在Lotes.get(..)打电话时你没有给方法'get'一个id

所以,我认为,您的方法调用应该类似于

Lotes.get({id: SOME_Id}, function(response){
    // ...do stuff with response
});

我不完全确定该语法,因为我个人更喜欢$q服务,因为它提供了更大的灵活性,但这就是您的代码通常出现的问题,您没有为您的方法提供它需要的参数( ID)。

另外,请记住在进行异步调用时使用 Angular 的$timeout服务。

于 2013-06-21T20:29:02.220 回答