2016-01-21 50 views
0

我在页面上有一个选择,我想用来自服务器的数据填充。我正在使用服务来检索这些记录,但是如何从promise中访问这些值并将它们放入选择标记的ng选项中?

从资源获取数据:

$scope.categories = Category.all({sorting:"asc"}); 

资源:

factory('Category', function ($resource) { 
    return $resource('api/categories/:id', {}, { 
     all: {method: "GET", isArray: true, params: {sorting: '@sorting'}}, 
     update: { 
      method: "PUT", 
      params: { 
       id: "@id" 
      } 
     } 
    }) 
}). 

回答

1

Category.all()呼叫应该返回将充满所检索的值的数组当相应的HTTP请求回报。如果你想运行在完成一些代码,你可以传递一个回调是这样的:

$scope.categories = Category.all({sorting:"asc"}, function() { 
    // do something with the $scope.categories 
}); 

您还可以得到这样的承诺:

$scope.categories = Category.all({sorting:"asc"}) 
    .$promise.then(function(categories) { 
     // do something with the categories 
    });