使用AngularJS打印JSON值

Print JSON value using AngularJS

本文关键字:JSON 打印 AngularJS 使用      更新时间:2023-09-26

我正在尝试使用AngularJS从JSON端点打印一些数据。终点:- http://localhost:8081/api

JSON结构:-
{
  "1": {
    "venture": "XYZ Informatics",
    "member": [
      {
        "name": "abcd",
        "email": "abcd@gmail.com"
      }
    ],
    "message": "This is good day",
    "isclicked": false
  },
  "2": {
    "venture": "BBC Informatics",
    "member": [
      {
        "name": "xyz",
        "email": "xyz@gmail.com"
      }
    ],
    "message": "This is bad day",
    "isclicked": false
  }
}

我想在行中显示企业s的名称。我期望的行输出是:-

XYZ Informatics
BBC Informatics

My Code is:-

    <!DOCTYPE html>
<html ng-app="MyApp">
  <head>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
    <style>
      table, th, td {
      border: 1px solid black;
      border-collapse: collapse;
      }
      th, td {
      padding: 15px;
      }
    </style>
  </head>
  <body>
    <div ng-app="MyApp" ng-controller="displayController">
      <table style="width:100%">
        <tr ng-repeat="data in datas">
          <td>{{ data.venture }}</td>
        </tr>
      </table>
    </div>
    <script>
      angular.module('MyApp', [])
      .controller('displayController', function($scope, $http) {
      var url = "http://localhost:8081/api";
      $http.get(url).success(function (response) {
      $scope.datas = response;
      });
      }
    </script>
  </body>
</html>

但是值没有显示。可能,我错过了进入JSON的每个数组。

您的脚本有语法错误。为你的控制器关闭括号

  <script>
    angular.module('MyApp', [])
      .controller('displayController', function($scope, $http) {
        //changed to your local api
         var url = "http://localhost:8081/api";
        $http.get(url).success(function(response) {
          $scope.datas = response;
        });
      });//this is the place where you miss out the closing bracket
  </script>