2016-04-01 10 views
2

私のAngularアプリケーションでは、odatasources.jsライブラリを使用して公開ODataフィードからデータを取得しています。 、私は(スターターのために)やりたい何odataresources.jsライブラリを使用してAngularJSアプリケーションでJSの約束を正しく解決する方法

var myModule = angular.module("MyModule",['ODataResources']); 
myModule.controller("MyController",function($scope,$odataresource){ 
    $scope.results = 
     $odataresource("http://services.odata.org/V4/Northwind/Northwind.svc/Products") 
     .odata() 
     .filter('UnitPrice','>',10) 
     .filter('Discontinued',true) 
     .orderBy('UnitsInStock','asc') 
     .expand('Category') 
     .query(); 
}); 

です: 次の呼び出しは、この約束によって返された実際のデータへのアクセスを得るために、私は解決することができるようにしたいの約束を返します返されたデータを反復し、シンプルなコンソールにそれをログに記録する:

angular.forEach($scope.results, function(value, key) { 
     console.log(key + ' ' + value); 
    }); 

それは、今$ scope.resultsスタンドとしては、未解決の約束です。

この約束を解決し、実際に返されるデータを反復処理するための正しい構文は何ですか?ここで

は私の現在のバイオリンです:

http://jsfiddle.net/eugene_goldberg/gmqvq9fx/1/

答えて

1

あなたがquery()メソッドにコールバックに渡すことによってこれを行うことができます。 Fiddle

var myModule = angular.module("MyModule",['ODataResources']); 
myModule.controller("MyController",function($scope,$odataresource){ 
    $scope.results = 
     $odataresource("http://services.odata.org/V4/Northwind/Northwind.svc/Products") 
     .odata() 
     .filter('UnitPrice','>',10) 
     .filter('Discontinued',true) 
     .orderBy('UnitsInStock','asc') 
     .expand('Category') 
     .query(function(data) { 
      console.log('success'); 
      console.log(data); 
      angular.forEach(data, function(value, key) { 
      console.log(key + ' ' + value); 
      }); 
     }, function(err) { 
      console.log('There was an error: ', err); 
     }); 
}); 
関連する問題