2013-06-21 43 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

你可以展開爲什麼我必須使用$超時服務?如果沒有它,它似乎現在工作得很好。 –