【问题标题】:ng-repeat showing only the last item?ng-repeat 只显示最后一项?
【发布时间】:2016-11-12 10:50:45
【问题描述】:

我有一个基本的 API 调用,它返回一个对象。我想遍历对象以显示每个冠军播放的会话数,我的代码是:

          for( var c = 0; c < json['champions'].length; c++) {
        data.sessP = json['champions'][c]['stats'].totalSessionsPlayed;
        console.log(json['champions'][c]['stats'].totalSessionsPlayed);
      }

在我的页面中:

 <span ng-repeat="sessP in info.sessP">
    {{info.sessP}}
  </span>

在控制台中,正确输出了大约 6 或 7 个 totalSessionsPlayed 的列表,所以我知道它在那里,但是在页面上,只显示输出的最后一个结果:

http://imgur.com/a/yj81f

我做错了吗?我假设我正在正确访问该对象,因为页面上显示了一个值,而不是全部:(

谢谢

【问题讨论】:

  • 你应该使用{{sessP}} 并从中获取其他东西
  • 什么是 info.sessP ?
  • info 是数据在推送到页面之前绑定的内容。我使用 mustache 作为模板引擎。信息本质上是数据。
  • 我改为: {{info.sessP}} 仍然只显示数字 11?为什么它不会重复每个结果?啊啊!
  • 为什么在 Angular 应用程序中需要小胡子?或者你为什么还要使用它?请提供minimal reproducible example

标签: angularjs json node.js


【解决方案1】:

你应该像这样绑定数组...

$scope.sessP  = [];
for( var c = 0; c < json['champions'].length; c++) {
    $scope.sessP.push(json['champions'][c]['stats'].totalSessionsPlayed);
    console.log(json['champions'][c]['stats'].totalSessionsPlayed);
}

HTML:

<span ng-repeat="sesP in sessP">
    {{sesP}}
</span>

【讨论】:

  • 不幸的是,现在什么都没有显示。感谢您的尝试!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多