小编Pav*_*ala的帖子

从angularJS $资源中提取数据

我在使用Angular的$ resource服务时遇到了问题.由于我的API发回完整的响应(标题等)以及数据对象中的错误字段,我需要从响应中获取特定对象.使用$ http服务这很简单,但我不明白在哪里使用$ resource服务获取这些参数.以下是工作$ http请求.

$http({
  method  : 'GET',
  url     : 'http://api.discussorama.com/v1/topics',
  headers : {'X-Api-Secret': 'xxx', 'Authorization': 'xxx', 'Content-Type': 'application/x-www-form-urlencoded'}  
})
.then(function(response) {
  console.log(response);
  $scope.topics = response.data.topics;
});
Run Code Online (Sandbox Code Playgroud)

这是我在使用$ resource的同一请求时失败的attemt:

var Topic = $resource('http://api.discussorama.com/v1/topics/:id', {id: '@id'}, {
  query: {
    isArray: false,
    method: 'GET',
    headers: {'X-Api-Secret': 'xxx', 'Authorization': 'xxx', 'Content-Type': 'application/x-www-form-urlencoded'}
  }
 });

var response = Topic.query();
$scope.topics = response.topics;
console.log(response);
Run Code Online (Sandbox Code Playgroud)

编辑:更新上面的代码后,这是我在控制台中得到的.

f {$promise: Object, $resolved: false, $get: function, $save: function, $query:    function…}
  $promise: Object
  $resolved: true
  error: false
  topics: Array[10] …
Run Code Online (Sandbox Code Playgroud)

rest angularjs ngresource

3
推荐指数
1
解决办法
6739
查看次数

标签 统计

angularjs ×1

ngresource ×1

rest ×1