Как сделать http (ng-repeat) с вложенным json в угловой js? - PullRequest
0 голосов
/ 23 января 2019

Мой код работает только тогда, когда мой JSON не вложен.Когда между данными нет ",", и я использую только один блок JSON, он работает.

My Angular:

<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"
   }
 ]

Спасибо

1 Ответ

0 голосов
/ 24 января 2019

Пожалуйста, используйте $scope.info=response.data; вместо $scope.info=response;.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...