将JSON数据压缩为angularjs

时间:2016-02-01 05:09:43

标签: angularjs

我正在尝试将json文件中的数据读入angular.js,但我没有获取数据。

[
  {
    "code": "AD",
    "name": "Andorra",
    "population": "84000"
  },
  {
    "code": "TN",
    "name": "Tunisia",
    "population": "10589025"
  },
  {
    "code": "TW",
    "name": "Taiwan",
    "population": "22894384"
  },
  {
    "code": "TZ",
    "name": "Tanzania",
    "population": "41892895"
  },
  {
    "code": "UA",
    "name": "Ukraine",
    "population": "45415596"
  },
  {
    "code": "UG",
    "name": "Uganda",
    "population": "33398682"
  },
  {
    "code": "UM",
    "name": "U.S. Minor Outlying Islands",
    "population": "0"
  },
  {
    "code": "US",
    "name": "United States",
    "population": "310232863"
  },
  {
    "code": "UY",
    "name": "Uruguay",
    "population": "3477000"
  },
  {
    "code": "UZ",
    "name": "Uzbekistan",
    "population": "27865738"
  },
  {
    "code": "VA",
    "name": "Vatican City",
    "population": "921"
  },
  {
    "code": "VC",
    "name": "Saint Vincent and the Grenadines",
    "population": "104217"
  },
  {
    "code": "VE",
    "name": "Venezuela",
    "population": "27223228"
  },
  {
    "code": "VG",
    "name": "British Virgin Islands",
    "population": "21730"
  },
  {
    "code": "VI",
    "name": "U.S. Virgin Islands",
    "population": "108708"
  },
  {
    "code": "VN",
    "name": "Vietnam",
    "population": "89571130"
  },
  {
    "code": "VU",
    "name": "Vanuatu",
    "population": "221552"
  },
  {
    "code": "WF",
    "name": "Wallis and Futuna",
    "population": "16025"
  },
  {
    "code": "WS",
    "name": "Samoa",
    "population": "192001"
  },

  {
    "code": "ZW",
    "name": "Zimbabwe",
    "population": "11651858"
  }
]

的index.html



<html ng-app="countryApp">
  <head>
    <meta charset="utf-8">
    <title>Angular.js JSON Fetching Example</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
	<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
    <script>
      var countryApp = angular.module('countryApp', []);
      countryApp.controller('CountryCtrl', function ($scope, $http){
        $http.get('countries.json').success(function(data) {
          $scope.countries = data;
        });
      });
    </script>
  </head>
  <body ng-controller="CountryCtrl">
	<h2>Angular.js JSON Fetching Example</h2>
    <table>
      <tr>
        <th>Code</th>
		<th>Country</th>
        <th>Population</th>
      </tr>
      <tr ng-repeat="country in countries">
        <td>{{country.code}}</td>
		<td>{{country.name}}</td>
        <td>{{country.population}}</td>
      </tr>
    </table>
  </body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

当你进行$ http.get调用时,回调函数返回响应对象。这意味着您需要访问响应的data属性,而不仅仅是分配响应:

$http.get('countries.json').success(function(response) {
  $scope.countries = response.data;
});

查看$ http.get的文档,特别是示例($http docs

答案 1 :(得分:0)

请检查json文件的路径,因为代码没问题。

如果从本地计算机执行代码,则需要服务器来运行nodejs之类的代码。