2013-06-21 47 views
1

好的,所以我想我在这里缺少一些基本的东西,但我无法想象它读取文档和其他示例。我有这样一个工厂的资源:AngularJS资源工厂总是返回空的响应

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的作品时,我手动定义ID这样$resource('./api/lotes/21'工厂,但我已经尝试添加params:{id:"@id"},但那也没有工作。

回答

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

documentation

var User = $resource('/user/:userId', {userId:'@id'}); 
var user = User.get({userId:123}, function() { 
    user.abc = true; 
    user.$save(); 
}); 
1

我认为你的问题你是说有'get'方法(id)的参数,但是你没有给我的ThOD“GET”当你让你在通话Lotes.get(..)

因此,一个ID,我想,你的方法调用应该是沿着

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

我不完全的东西线当然,我个人更喜欢$q服务,因为它提供了更多的灵活性,但这就是一般情况下你的代码出了问题,你没有给你的方法提供它需要的参数(一个id)。

此外,请记住要使用您的Angular的$timeout服务,因为您正在进行异步调用。

+0

啊刚看到@moderndegree的帖子。所以我想我的语法是正确的,但仍然记得$超时服务,你将需要在一秒钟。 – hunt

+0

$超时的好处。 –

+0

你可以展开为什么我必须使用$超时服务?如果没有它,它似乎现在工作得很好。 –