如何使用 AngularJS 访问 JSON 对象

MVC

我已将 JSON 存储到视图中Countries.Json调用的文件夹内的单独文件ListofCountries

现在我想使用AngularJS.

但我无法将 JSON 值传递到我的代码中。

请帮我。

国家.json

{
  "Countries": [
    {
      "Country": "USA"
    },
    {
      "Country": "Australia"
    },
    {
      "Country": "Canada"
    },
   {
      "Country": "UK"
    }
  ]
}

HTML代码

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="countriesCtrl"> 

<ul>
  <li ng-repeat="x in myData">
    {{ x.Country }}
  </li>
</ul>
<input type="button" id="list-btn" value="List Data"/>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('countriesCtrl', function($scope, $http) {
  $http.get("Countries.Json").then(function (response) {
      $scope.myData = response.data.records;
  });
});
</script>

</body>
</html>
萨吉塔兰

您需要访问response.Countriesresponse.data.records

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
  $http.get("Countries.json")
  .success(function (response) {$scope.code = response.Countries;})
  .error(function (response) {alert("Error")})
});

WORKING DEMO

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章