【问题标题】:AngularJs ng-repeat not rendering data yet http 200 responseAngularJs ng-repeat 尚未呈现数据 http 200 响应
【发布时间】:2019-05-15 04:38:00
【问题描述】:

这是一份学术作业。这是我第一次使用 Angularjs,我正在尝试在网页上显示数据。在 Chrome 控制台上,我收到一个 http 响应代码 200,这意味着我已成功获取数据,但它似乎没有显示。

groups.html:

<html>

    <head>
        <script src = "groups.js"></script>
    </head>
<body>
<div ng-app="myApp">
<div ng-controller="groupsCtrl">
    <div class="group-jumbotron">
        <h1 class="display-4">Champion's League Groups</h1>
        <p class="lead">The 2018–19 UEFA Champions League group stage began on 18 September and is scheduled to end on 12 December 2018. <br/>
            A total of 32 teams compete in the group stage to decide the 16 places in the knockout phase of the 2018–19 UEFA Champions League.</p>
        <hr class="my-1">
        <p>Information about each group can be seen below</p>
    </div>
     <div class="addGroup-Title">
        <h4 class="display-6">Groups:</h4>

          <table>
      <thead>
        <tr>
            <th>Group Letter</th>
            <th>Number of Teams</th>
            <th>Matches Played</th>
            <th>Top Goalscorer</th>
            <th>Top Assists</th>
            <th>Most Cards</th>
            <th>Total Points</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="group in leagueGroup">
            <td>{{group.groupLetter}}</td>
            <td>{{group.numberOfTeams}}</td>
            <td>{{group.totalMatchesPlayed}}</td>
            <td>{{group.topGoalscorer}}</td>
            <td>{{group.topAssists}}</td>
            <td>{{group.mostCards}}</td>
            <td>{{group.totalPoints}}</td>
        </tr>
      </tbody>
    </table>
     </div>
    <div class="addGroup-Title">
        <h4 class="display-6">Add a New Group:</h4>


    <form ng-show="showForm" ng-submit="submitNewGroupForm()" style="margin-left: 10px;margin-right: 10px" ng-model="newGroup">
            <div class="form-row">
                <div class="form-group col-md-3">
                    <label for="AddGroupLetter">Group Letter:</label>
                    <input type="text" ng-model="newGroup.groupLetter"class="form-control" id="AddGroupLetter" min="1" max="2" required>
                </div>
                <div class="form-group col-md-3">
                    <label for="AddnumTeams">Number of Teams:</label>
                    <input type="number" class="form-control" id="AddnumTeams" ng-model="newGroup.numberOfTeams" required>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="AddTotalMatches">Total Matches Played:</label>
                    <input type="number" class="form-control" id="AddTotalMatches" ng-model="newGroup.totalMatchesPlayed" required>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="AddTotalPoints">Total Points:</label>
                    <input type="number" class="form-control" id="AddTotalPoints" ng-model="newGroup.totalPoints" required>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-3">
                    <label for="AddTopGoalscorer">Top Goalscorer:</label>
                    <input type="text" class="form-control" id="AddTopGoalscorer" ng-model="newGroup.topGoalscorer" required>
                </div>
                <div class="form-group col-md-3">
                    <label for="AddTopAssists">Top Assists:</label>
                    <input type="text" class="form-control" id="AddTopAssists" ng-model="newGroup.topAssists" required>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="AddMostCards">Most Cards</label>
                    <input type="text" class="form-control" id="AddMostCards" ng-model="newGroup.mostCards" required>
                </div>
            </div>
            <button type="submit" class="btn btn-primary btn-lg">Create A Group</button>
        </form>
    </div>

    <div class="addGroup-Title">
        <h4 class="display-6">Delete a Group:</h4>

        <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="DeleteGroup">ID of Group</label>
                    <input type="text" class="form-control" id="DeleteGroup" ng-model="groupData.groupId" required>
                </div>
            </div>
            <button type="submit" class="btn btn-primary btn-lg">Delete A Group</button>

    </div>
    </div>
</div>
    </body>
</html>

groups.js:

    'use strict';

angular.module('myApp.groups', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/groups', {
        templateUrl: 'groups/groups.html',
        controller: 'groupsCtrl'
    });
}])

.controller('groupsCtrl', function ($scope, $http) {
    $scope.leagueGroup = [];
    $http.get('http://localhost:5000/api/v1/groups')
        .then(function (response) {
            $scope.leagueGroup = response.data;
        });

    $scope.submitNewGroupForm = function () {
        $scope.newGroup =
            {
                groupId: getgroupId(),
                groupLetter: $scope.newGroup.groupLetter,
                numberOfTeams: $scope.newGroup.numberOfTeams,
                totalMatchesPlayed: $scope.newGroup.totalMatchesPlayed,
                topGoalscorer: $scope.newGroup.topGoalscorer,
                topAssists: $scope.newGroup.topAssists,
                mostCards: $scope.newGroup.mostCards,
                totalPoints: $scope.newGroup.totalPoints
            };

        $http.post('http://localhost:5000/api/v1/groups', $scope.newGroup)
            .then(function (response) {
                $scope.response = response.data;
                  alert('Created new Group: ' + $scope.response.stadiumName);
            });
    };

    $http.delete('http://localhost:5000/api/v1/groups/'+ $scope.groupData.groupId)
                .then(function (response) {
                    $scope.response = response.data;
                });
    });
        function getgroupId() {
            return Math.floor((Math.random() * 9999) + 10);
        }

数据示例:

groupId: 1
groupLetter: "A"
mostCards: "Marcos Rojo"
numberOfTeams: 4
topAssists: "Kevin De Bruyne"
topGoalscorer: "Cristiano Ronaldo"
totalMatchesPlayed: 6
totalPoints: 48

响应(chrome dev):

[  
   {  
      "groupId":1,
      "groupLetter":"A",
      "mostCards":"Marcos Rojo",
      "numberOfTeams":4,
      "topAssists":"Kevin De Bruyne",
      "topGoalscorer":"Cristiano Ronaldo",
      "totalMatchesPlayed":6,
      "totalPoints":48
   },
   {  
      "groupId":2,
      "groupLetter":"B",
      "mostCards":"Ander Herrera",
      "numberOfTeams":4,
      "topAssists":"Luka Modric",
      "topGoalscorer":"Sergio Aguero",
      "totalMatchesPlayed":6,
      "totalPoints":36
   },
   {  
      "groupId":3,
      "groupLetter":"C",
      "mostCards":"Sergio Ramos",
      "numberOfTeams":4,
      "topAssists":"Xavi",
      "topGoalscorer":"Lionel Messi",
      "totalMatchesPlayed":6,
      "totalPoints":50
   },
   {  
      "groupId":4,
      "groupLetter":"D",
      "mostCards":"Virgil Van Dijk",
      "numberOfTeams":4,
      "topAssists":"Neymar",
      "topGoalscorer":"Kylian MBappe",
      "totalMatchesPlayed":6,
      "totalPoints":32
   }
]

【问题讨论】:

  • 几个问题:你能举个例子说明response.data是什么吗?您是否在该响应处理函数中设置了断点来检查数据?您是否能够在视图中呈现来自该控制器的其他数据?
  • console.log 你的 LeagueGroup 以查看返回的内容以及它是否与 ng-repeat 的预期结构匹配。 Alo - 它可能只是你发布的 ocde - 但你没有关闭 Group,js 中的 .controller 声明
  • 是的,它只是我发布的代码。生病张贴数据是什么样子的屏幕截图。我对此不太有信心,所以没有设置任何断点或任何东西
  • 您是否在代码中的任何位置使用 ng-controller?
  • @JamesBrunet 是的,我更新了我上面的 html

标签: javascript html angularjs http angularjs-ng-repeat


【解决方案1】:

您是否对响应对象进行了console.log 以确保它符合您的预期?

在这里做:

.then(function (response) {
        console.log(response);
        $scope.leagueGroup = response.data;
        $scope.$apply();
    });

您是否尝试在分配数据后执行 $scope.$apply()?

另一个确认您的数据是否正确的选项是在您的模板中简单地放置一个&lt;pre&gt; 标记以确认数据已绑定,如下所示: &lt;pre&gt;{{leagueGroup}}&lt;/pre&gt;

【讨论】:

  • 我正在使用 pycharm,你能告诉我如何根据我的代码执行 console.log 吗?还有申请?
  • 你应该打开浏览器调试工具。右键单击 -> 在 chrome 上检查。
  • @AaronRogers 我询问 console.log 的原因是预感您绑定的数据可能不是正确的形状。
  • @jonnycraze 当我运行它时,我在哪里查看控制台?
【解决方案2】:

您的模块名称似乎有误。您已将其命名为 myApp.groups,但它应该只是 myApp

angular.module('myApp.groups', ['ngRoute'])

应该是:

angular.module('myApp', ['ngRoute'])

您是否在控制台中遇到任何错误?

const data = [{
    "groupId": 1,
    "groupLetter": "A",
    "mostCards": "Marcos Rojo",
    "numberOfTeams": 4,
    "topAssists": "Kevin De Bruyne",
    "topGoalscorer": "Cristiano Ronaldo",
    "totalMatchesPlayed": 6,
    "totalPoints": 48
  },
  {
    "groupId": 2,
    "groupLetter": "B",
    "mostCards": "Ander Herrera",
    "numberOfTeams": 4,
    "topAssists": "Luka Modric",
    "topGoalscorer": "Sergio Aguero",
    "totalMatchesPlayed": 6,
    "totalPoints": 36
  },
  {
    "groupId": 3,
    "groupLetter": "C",
    "mostCards": "Sergio Ramos",
    "numberOfTeams": 4,
    "topAssists": "Xavi",
    "topGoalscorer": "Lionel Messi",
    "totalMatchesPlayed": 6,
    "totalPoints": 50
  },
  {
    "groupId": 4,
    "groupLetter": "D",
    "mostCards": "Virgil Van Dijk",
    "numberOfTeams": 4,
    "topAssists": "Neymar",
    "topGoalscorer": "Kylian MBappe",
    "totalMatchesPlayed": 6,
    "totalPoints": 32
  }
];

const app = angular.module('myApp',[]);

app
.controller('groupsCtrl', ['$scope', '$http', '$timeout', function($scope, $http, $timeout) {

  $timeout(1000)
    .then(function() {
      $scope.leagueGroup = data;
    });

}])
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<html>

<body>
  <div ng-app="myApp">
    <div ng-controller="groupsCtrl">

      <div class="addGroup-Title">
        <h4 class="display-6">Groups:</h4>

        <table>
          <thead>
            <tr>
              <th>Group Letter</th>
              <th>Number of Teams</th>
              <th>Matches Played</th>
              <th>Top Goalscorer</th>
              <th>Top Assists</th>
              <th>Most Cards</th>
              <th>Total Points</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat="group in leagueGroup">
              <td>{{group.groupLetter}}</td>
              <td>{{group.numberOfTeams}}</td>
              <td>{{group.totalMatchesPlayed}}</td>
              <td>{{group.topGoalscorer}}</td>
              <td>{{group.topAssists}}</td>
              <td>{{group.mostCards}}</td>
              <td>{{group.totalPoints}}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</body>

</html>

【讨论】:

  • 太好了,很高兴听到!请随时接受答案。
  • 我的意思是你的代码适用于 sn-p.. 我的问题仍然存在:(
【解决方案3】:

在我的groups.js 中经过大量的敲打之后,我在相同的函数中调用了我的所有方法,例如http.get。它们需要在单独的函数上调用,以便在屏幕加载时它们都不会运行。一旦我重新组织成单独的功能,数据就会显示出来。

感谢大家的帮助

【讨论】:

  • 如果其他人遇到这种情况,您能否发布您的最终代码以帮助其他人?
猜你喜欢
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-27
  • 2018-01-17
相关资源
最近更新 更多