Angular.js不显示从$检索的对象数组http.get [英] Angular.js not displaying array of objects retrieved from $http.get

查看:144
本文介绍了Angular.js不显示从$检索的对象数组http.get的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来angular.js,我想了解如何使用的$ HTTP的功能,如 $ http.get $ HTTP .POST

I am new to angular.js and I am trying to understand how to use the $http functions like $http.get and $http.post.

我有一个非常简单的应用程序通过Java REST服务检索从MySQL数据库的消息。返回的数据是这样的:

I have a very simple app retrieving messages from a MySQL database through a Java rest service. The data returned looks like:

[
  {id:1, content:"Hello World!"}, 
  {id:2, content:"Testing 1, 2, 3..."}, 
  {id:3, content:"I am from the database"}
]

我试图用一一列举纳克重复像这样:

I am attempting to list them using ng-repeat like so:

<div ng-controller="MessageController">
  <ul>
    <li ng-repeat="m in messages">{{m.content}}</li>
  </ul>
</div>

然后我的控制器看起来像这样:

And then my controller looks like so:

function MessageController($scope, $http) {
  $scope.getAll = function(callback) {
    $http.get('/messages').success(function(response) { callback(response); });
  }

  $scope.messages = $scope.getAll(function(data) { return JSON.stringify(data); });
}

我看到调用正常返回的信息对象的数组,我可以将它们从回调函数中打印到控制台,但他们没有被显示在某些原因NG-重复。我能得到这个,当我手动创建的邮件列表的工作,但是当我介绍了 $ http.get 片,它停止工作。

I see the call is properly returning an array of message objects and I can print them to the console from within the callback function but for some reason they are not being displayed in the ng-repeat. I was able to get this to work when I was manually creating a list of messages, but when I introduced the $http.get piece, it stopped working.

我缺少的东西吗?

编辑:我使用的版本angular.js 1.2.19

I am using angular.js version 1.2.19

推荐答案

根据AngularJS 1.2版本,阵列不再从无极解开(默认)(见的移民说明)。我已经看到了它与对象仍在工作,但根据你不应该依赖,无论是文档。

As per AngularJS version 1.2, arrays are not unwrapped anymore (by default) from a Promise (see migration notes). I've seen it working still with Objects, but according to the documentation you should not rely on that either.

相反,你应该使用 $资源这一点。资源不返回的承诺了,但未来的对象。在实践中,这意味着依赖于REST调用,填补自己曾经的REST调用做出决议(例如空对象或数组)。

Instead, you should use $resource for this. Resources don't return Promises anymore, but 'future' objects. In practice that means an empty object or array depending on the REST call, which fills itself once the REST call resolves (example).

在你的情况下,它会像下面这样(伪):

In your case it would be something like the following (pseudo):

function MessageController($scope, $resource) {
    var resource = $resource('/messages');
    $scope.messages = resource.query(function (data) {
        return JSON.stringify(data); // is this really necessary, though?
    });
    // $scope.messages = resource.query(); // should be enough
}

您将需要ngResource模块依赖为(不要忘记在您的index.html角resource.js):

You will need the ngResource module dependency for that (don't forget to include angular-resource.js in your index.html):

var app = angular.module('myApp', ['ngResource'])

这篇关于Angular.js不显示从$检索的对象数组http.get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆