javascript - AngularJS - API 调用返回 [object object]

标签 javascript json angularjs

我正在尝试从 NPR API 调用文章列表。我有一个以 JSON 形式返回的工作 URL。然而,在我的 Controller 中的某个地方,我迷失了获取该对象的方向。当我 console.log 它进行测试时,它返回为 [object Object] 而没有其他内容。我的服务如下所示:

app.factory('nprService', function($resource) {
//call npr api
return $resource('http://api.npr.org/queryid=61&fields=title,byline,text,image,all&output=JSON...

和我的 Controller :

app.controller('ArticleListCtrl', function($scope, nprService) {
//call the service and store as a variable
$scope.article = nprService.get();
});

我尝试使用查询来获取结果,如下所示,但它返回单个 JSON 对象,因此显然不起作用。

//call the service and store as a variable
nprService.query(function(data) {
    $scope.article = data;
});

任何帮助将不胜感激。提前致谢。

最佳答案

您需要使用 Promise 构造来获取数据。 Controller 代码可以重写为:

app.controller('ArticleListCtrl', function($scope, nprService) {
//call the service and store as a variable
nprService.get().then(function(result){
    $scope.article = result.data;
});

关于javascript - AngularJS - API 调用返回 [object object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24749655/

相关文章:

javascript - Angular 标签指令问题

java - 从mongodb中提取特定字段

javascript - 解析 HTML 表的 JSON 对象

javascript - MooTools 获取类中的类名(而不是 CSS)?

javascript - 在 CouchDB 中使用 Mapreduce 方法链接文档

ruby-on-rails - 如何在 rails 3 功能测试中发布 JSON 数据

javascript - 使用 AngularJS 登录 Spring Boot $http.post

angularjs - ionic 应用程序启动事件

javascript - 恢复或暂停cordova事件后如何解决?

javascript - 使 HTML 表格适合 Bootstrap 对话框