AngularJS:ng-repeat不显示JSON [英] AngularJS: ng-repeat not displaying JSON

查看:71
本文介绍了AngularJS:ng-repeat不显示JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让Angular显示我已经设法通过PDO从数据库中提取的JSON数据. PDO部分和JSON编码部分工作正常-控制台正在按预期返回数据.

I'm trying to get Angular to display JSON data that I've managed to pull from a database via PDO. The PDO part and JSON encode part is working fine -- console is returning the data as expected.

但是,当使用ng-repeat时,会显示div,但是不会显示post.time.

However, when using ng-repeat the divs do display but post.time does not show.

HTML

<html ng-app="dataVis">
    . . .
    <body ng-controller="GraphController as graph">
        <div ng-repeat="post in graph.posts track by $index">
            {{post.time}}
        </div>
    </body>
</html>

JSON数据

[
    {
        "time": "1340",
        "postId": "282301",
        "likes": "2"
    },
    {
        "time": "1300",
        "postId": "285643",
        "likes": "0"
    }
] . . . (etc)

JS

(function () {

  var app = angular.module('dataVis', []);

  app.controller('GraphController', ['$http', function ($http) {
    var graph = this;
    graph.posts = [];
    $http.get('/query-general.php').success(function (data) {
      console.log(data); // returns JSON data
      graph.posts = data;
    });

  }]);

}());

起初我没有包含track by $index,但是在收到重复错误时,我决定将其包含在内.

At first I did not include track by $index but upon receiving a dupes error I decided to include it.

我想使用ng-repeat在HTML页面中显示JSON数据.有人可以伸出援助之手来完成这项工作吗?

I would like to display the JSON data in the HTML page using ng-repeat. Can anybody lend a helping hand to get this working?

推荐答案

我解决了该问题.我在php标记之外的php文件中有注释.因此,当angular尝试从php页面获取JSON数据时,注释也会被提取.感谢您的所有帮助.

I resolved the issue. I had comments in my php files outside of the php tags. So when angular tries to get the JSON data from the php page, the comments were fetched too. Thanks for all the help.

这篇关于AngularJS:ng-repeat不显示JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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