【问题标题】:Angular js foreach only returning last item in arrayAngular js foreach 仅返回数组中的最后一项
【发布时间】:2014-11-11 05:58:52
【问题描述】:

我正在尝试使用angular.forEach() 从 JSON 返回每个项目对象并评估其值,但只返回最后一个项目。因此,我无法进行任何评估。有趣的是,如果我使用console.log(),它会一个一个地显示每个项目。

我怎样才能得到每个项目并评估它们?

如果你知道更好的方法,请教我。

JS(angularjs):

angular.module('bLiApp')
  .controller('AddDataCtrl', ['$scope', 'DataService', function ($scope, DataService) {

    DataService.getItems().success(function(data) {

      $scope.items = data.category.items;

      angular.forEach($scope.items, function(item) {

        // This is where I'm having problem with return data...
        if (item.amount < 600) {
          $scope.amountchecker = 'low';
        } else if (item.amount >= 600 && item.amount <= 650) {
          $scope.amountchecker = 'average';
        } else if (item.amount > 650) {
          $scope.amountchecker = 'too high';
        }

      });
    });
  }]);

HTML (angularjs):

<div class="add-data" ng-controller="AddDataCtrl">
  <div class="data-box clearfix" ng-repeat="item in items">
    <article class="item">
      <p>{{amountchecker}}</p>
      <p><span class="month">{{ item.month.substring(0, 3) }}</span></p>
      <p class="cost">{{item.cost | currency:"&pound;"}}</p>
    </article>
  </div>
</div>

非常感谢

【问题讨论】:

  • 你可以尝试在 ng repeat 中添加“track by $index”吗?
  • 你不是想说item.amountchecker = ''吗?因为你每次都在那里覆盖它。 (以及 html 中的 {{item.amountchecker}}
  • @Goodzilla,不,我正在测试一些东西。可以忽略。那么你知道发生了什么以及我该如何解决这个问题吗?
  • @ThomasP1988,对不起,我刚刚开始学习 Angular。 $index 是什么意思?
  • @Shaoz 如果您有重复的项目,请参阅documentation。你能告诉我们有问题的数组吗?

标签: javascript angularjs foreach angularjs-scope


【解决方案1】:

您实际上不需要 forEach 循环来实现:

<div class="add-data" ng-controller="AddDataCtrl">

 <div class="data-box clearfix" ng-repeat="item in items">
    <article class="item">
      <p>
         <span ng-if="item.amount < 600">Low</span>
         <span ng-if="item.amount >= 600 && <= 650">Average</span>
         <span ng-if="item.amount < 650">Too high</span>
      </p>
      <p><span class="month">{{ item.month.substring(0, 3) }}</span></p>
      <p class="cost">{{item.cost | currency:"&pound;"}}</p>
    </article>
  </div>
</div>

这应该可以完成工作。请注意,这只会显示数量,如果您想在模型中更改它(如果您必须发回刚刚评估的数据),您应该更改控制器中的数据:

  angular.forEach($scope.items, function(item) {
    item.amountChecker; // declare a new property for each item in $scope.items
    // then we set the value for each item
    if (item.amount < 600) {
      item.amountChecker = 'low';
    } else if (item.amount >= 600 && item.amount <= 650) {
      item.amountChecker = 'average';
    } else if (item.amount > 650) {
      item.amountChecker = 'too high';
    }

  });

这应该在您的 $scope.items 对象中添加一个新行,其中 amountChecker 将分配给每个项目。 然后,在您的 ng-repeat 中,您还可以显示该值:item.amountChecker

这一次,它将调用每个项目的属性amountChecker,而不是存储在$scope.amountchecker中的最后一个值。

请注意,Goodzilla 实际上是在评论中以更短的方式回答的:)

【讨论】:

  • 啊,我明白了。谢谢你。我只想在控制器而不是视图中进行所有评估。所以我不必用标签填充视图。但是有没有办法在 JS 中做到这一点。
  • 感谢您的回答和帮助。它现在工作正常,我明白你做了什么。非常感谢。
  • 最好的解决方案可能是filter
  • @Goodzilla,是的,我也是这么想的。
【解决方案2】:

在循环到$scope.items 的每次迭代中,您似乎都覆盖了$scope.amountchecker。如果$scope.items 有一个amount 为500 的项目,然后是一个amount 为650 的项目,$scope.amountchecker 最终将设置为“平均”,因为 650 是循环中遇到的最后一个项目的数量。

解决方案取决于您是否真的希望amountchecker 基于所有项目或单个项目。如果是后者,您将更改您的行,例如:

$scope.amountchecker = 'low';

item.amountchecker = 'low';

【讨论】:

  • 感谢您的回复和帮助。
  • 没问题!我刚刚看到其他人实际上首先在 cmets 中回答了这个问题......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 2013-03-15
  • 2023-03-11
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多