无法使用angularjs读取json

时间:2016-01-06 17:58:36

标签: javascript angularjs json

在下面的代码中,

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Dashboard</title>
        <!-- Libs -->
        <script src="angular.js"></script>
        <script>
        var countryApp = angular.module('countryApp', []);
        countryApp.controller('CountryCtrl', function ($scope, $http){
            $http.get('country_codes.json').success(function(data) {
                $scope.countries = data;
            });
        });
        </script>   
    </head>
    <body ng-controller="CountryCtrl">

    </body>
</html>

/ * country_codes.json * /

[
  {
    "code": "AD",
    "name": "Andorra",
    "population": "84000"
  },
  {
    "code": "AE",
    "name": "United Arab Emirates",
    "population": "4975593"
  }
]

根据调试,我没有看到任何错误,并且回调函数没有执行。

为什么控制器回调没有执行?

2 个答案:

答案 0 :(得分:2)

添加ng-app =&#34; countryApp&#34;在你的HTML标签中它适用于我。     

答案 1 :(得分:1)

添加错误功能,您将看到错误详细信息:

import Operators._

将json更改为:

<script>
        var countryApp = angular.module('countryApp', []);
        countryApp.controller('CountryCtrl', function ($scope, $http){
              $http.get('country_codes.json')
    .then(function(data) {$scope.countries = data;}
,
          function(data) {console.log("error: "+data)});
 });
 </script>