2013-11-21 68 views
9

因此,它看起來像例子,你可以這樣做:

App.controller('ProjectListCtrl', ['$scope', 'Restangular', function($scope, Restangular) { 
    $scope.projects = Restangular.all('project/').getList(); 
}]); 

但是,這並不爲我工作。當我NG-重複的項目的項目,並期待在範圍內,我看到:

{ 
projects: { 
    then: null 
    catch: null 
    finally: null 
    call: null 
    get: null 
    restangularCollection: true 
    push: null 
} 
} 

它看起來像一個未解決的承諾對象吧?

這工作得很好,但更詳細:

lgtApp.controller('ProjectListCtrl', ['$scope', 'Restangular', function($scope, Restangular) { 
    Restangular.all('project/').getList().then(function(projects){    
    $scope.projects = projects;            
    });                  
}]);                   

我錯過了什麼?這是在文檔:

$scope.owners = house.getList('owners') 

要不要緊,但這個時候,我在紋波鉻插件測試PhoneGap的應用情況。

回答

7

從角度1.2開始,自動解除承諾被禁用並且不推薦使用。所以,雖然

$scope.projects = Restangular.all('project/').getList(); 

將允許您以您的觀點在以前版本訪問projects直接(爲其Restangular文檔,其中可能寫的),將不再自動工作。您可以通過$parseProvider.unwrapPromises() API重新啓用該功能,但它已被棄用,因此您最好的選擇可能是手動解決控制器中的承諾,就像您在更詳細的示例中一樣。

查看登記commit message瞭解更多詳情。

9

作爲對其他評論的補充,儘管承諾展開被禁用,但我在Restangular中實現了一種新的承諾解包方式(類似於$資源沒有刪除承諾的情況)。檢查https://github.com/mgonto/restangular#using-values-directly-in-templates瞭解更多信息:)

+1

這應該是公認的答案 – yar1

+0

好像不是具有與此多少運氣。它返回一個空數組 - http://stackoverflow.com/questions/27844177/restangular-data-getting-into-scope-for-a-list – Horse

12
$scope.projects = Restangular.all('project/').getList().$object; 
+0

在當前版本(Restangular 1.5.1,Angular 1.3.15)這個方法似乎工作,因爲實際的數組是在屬性「$ object」中。 – methai