来自json文件的数据未在AngularJS中加载

时间:2016-01-26 13:22:04

标签: php angularjs json

为什么我的数据没有加载到表格中。我认为我在标签上做错了什么。它包含myModule,但我在脚本中设置了myApp。请查看我的代码,尝试了所有内容,但没有任何帮助。

这是我的index.php文件

<!DOCTYPE>
    <html xmlns="http://www.w3.org/1999/xhtml" ng-app="myModule">
    <head>
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
    <script>

        var myApp = angular.module("myModule", []);

        myApp.controller('myController',function($scope,$http){
            $http.get('data.json').success(function(data){
                $scope.countries = data;
        });

    });

    </script>
    </head>

    <body ng-controller="myController">

    <table border=1 cellpadding="5" cellspacing="0">
    <tr><td>Country</td><td>Capital</td></tr>
    <tr ng-repeat="country in countries"><td>{{ country.name }}</td><td>{{ country.capital }}</td></tr>
    </table>

    </body>
    </html>

这是我的data.json文件,位于index.php所在的文件夹中。

[

    {name: "India", capital: "New Delhi"},
    {name: "USA", capital: "Washington D.C."},
    {name: "Russia", capital: "Moscow"},
    {name: "France", capital: "Paris"},
    {name: "China", capital: "Beijing"},
    {name: "UK", capital: "London"}

]

表中仍然没有数据,请帮助。

1 个答案:

答案 0 :(得分:0)

您正在为您的国家/地区分配响应对象。您应该分配response.data。看看$http documentation

myApp.controller('myController',function($scope,$http){
    $http.get('data.json').success(function(response){
        //$scope.countries = response;
        $scope.countries = result.data;
});

这是我能看到的代码错误的一件事。但是,如果您的API无法正常工作,我将需要错误消息来帮助您进一步。