如何在角度js中使用嵌套的json执行http(ng-repeat)



我的代码仅在我的JSON未嵌套时才有效。当数据之间没有","并且我只使用一个 JSON 块时,它可以工作。

我的角度:

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> 
</script>
  <body>
    <div ng-app="myApp" ng-controller="myCtrl"> 
      <table border="1">
        <tr ng-repeat="thing in info" ng-if="thing.color!=null">
          <td>{{thing.color}}</td>
          <td>{{thing.category}}</td>
          <td>{{thing.type}}</td>
        </tr>
        <tr ng-repeat="thing in info" ng-if="thing.detail!=null">
          <td>{{thing.detail}}</td>
          <td>{{thing.item}}</td>
          <td>{{thing.value}}</td>
        </tr>
      </table>
      <button class="button" ng-click="click()">Button 1</button>
      <script>
            var app = angular.module('myApp', []);
            app.controller('myCtrl', function($scope, $http) {
                $scope.click = function() {
                    $http.get("json.js").then(function (response) {
                    $scope.info=response;
                    });
                };
            });
      </script>
    </div>
  </body>
</html>

还有我的 JSON

[
  {
    "color": "black",
    "category": "hue",
    "type": "primary"
  },
  {
    "detail": "white",       
    "item": "red",                                              
    "value": "silver"
   }
 ]

谢谢

请使用$scope.info=response.data;而不是$scope.info=response;

最新更新