2014-09-26 23 views
4

我從restangular docs理解,稱Resource.getList('subElement',{query:param})應當爲/resource/subelement?query=paramRestangular的GetList有兩個參數的設置而不是查詢頭PARAMS

我使用洗劫上軌API之上的號召,我試圖傳遞一些搜索參數,形式爲「q [field_eq] = foo」。

這裏有一個控制器:

.controller('PlaypenCtrl', function PlaypenCtrl(
    $scope, 
    Resource 
) { 
    'use strict'; 

    var qParams; 

    $scope.doIt = function() { 
    qParams = { 
     per_page: 10 
    }; 
    qParams['q[some_field_eq]'] = 'foo'; 
    console.log(qParams); //Object {per_page: 10, q[some_field_eq]: "foo"} 

    Resource.getList('subElement',qParams).then(function(list){ 
     console.log(list); 
    }); 

    }; 
}); 

當我打電話doIt方法,這會記錄:

Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'q[some_field_eq]' is not a valid HTTP header field name. 

這是有道理的,因爲很明顯這不是一個有效的HTTP標頭字段名稱。

問題是...爲什麼Restangular會試圖將qParams對象放入請求標頭中?根據文檔,這些應該是請求參數。

如果我參加了「子元素」,工作的事情如預期:

$scope.doThat = function() { 
    qParams = { 
     per_page: 10 
    }; 
    qParams['q[some_field_eq]'] = 'foo'; 
    console.log(qParams); //Object {per_page: 10, q[some_field_eq]: "foo"} 

    Resource.getList(qParams).then(function(list){ 
     //calls /resource?per_page=10&q%5Bsome_field_eq%5D=foo 
     console.log(list); //...a normal restangular list object, as expected 
    }); 
    }; 

我缺少什麼?

回答

2

getList方法的兩個參數是查詢參數和標題。爲了訪問subElement端點,您必須使用以下內容:

Resource.all('subElement').getList(qParams) 

我相信這就是您要找的。

相關問題