【问题标题】:Display array items(json) using For Loop, If Statement and ng-repeat使用 For 循环、If 语句和 ng-repeat 显示数组项(json)
【发布时间】:2016-11-22 19:19:19
【问题描述】:

我正在尝试过滤两个 JSON 数组对象,以显示从数组中选择的结果。我正在使用两个 For 循环和一个 If 语句来过滤掉需要显示的内容。

但是我的问题在于 If 语句似乎没有过滤掉 For 循环的结果。显示 JSON 对象中的每个项目。请帮忙。

角度/javascript

      var url = generateQuery('GET', '/products', CONFIG, params);
      $scope.result = "";
      var i;
      var j;
      $scope.items = [];
      $http.get(url)
          .success(function(data, status, headers, config) {
              $scope.result = data;

              for (i = 0; i < $scope.result.products.length; i++) {

                  for (j = 0; j < $scope.result.products[i].categories.length; j++) {
                      $scope.category_limit = $scope.result.products[i].categories[j];

                      if ($scope.category_limit = 'Jewellery') {

                          $scope.items.push({
                              image: $scope.result.products[i].featured_src,
                              name: $scope.result.products[i].title,
                              price: '$' + $scope.result.products[i].price + ' (R' + ($scope.result.products[i].price * $scope.convert.results.rate.Rate).toFixed(2) + ')',
                              id: $scope.result.products[i].id,
                              cat: $scope.category_limit
                          });
                          $ionicLoading.hide();

                      } else {
                          $ionicLoading.hide();
                      }
                  }

              }

          })

html

        <div class="category-col" ng-repeat="i in items" id="swap_row_col">

            <a ng-href="#/app/products/{{i.id}}" href="#/app/products/{{i.id}}" class="no-underline">
                <img src= {{i.image}} class="category-image-col">      
                <!--<img src='images/add_to_cart.png' style="height: 24px; width: 28px; z-index: 5; position: fixed; margin-left: 34%; margin-top: -13%;">                                         -->
                <div class="category-name-col"> {{i.name}} </div>
                <div class="category-price-col"> {{i.price}} </div>
                <div class="category-price-col"> {{i.cat}} </div>
            </a>

        </div>

【问题讨论】:

  • 您能否详细说明返回的data 是什么样的?
  • 难道“珠宝”应该是“珠宝”?在没有看到您的数据的情况下,我们只是在这里猜测。

标签: javascript angularjs arrays json ionic-framework


【解决方案1】:

我认为您缺少 == 符号 if ($scope.category_limit == '珠宝'){ ...... }

【讨论】:

  • 好吧,看来我收到的数据对象是问题所在。每个 Products 对象没有相同数量的类别项目。 $scope.category_limit == 'Jewellery' 是正确的,谢谢。我会将其标记为正确答案,因为它将解决相关问题
猜你喜欢
  • 2011-11-28
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2021-03-18
  • 1970-01-01
相关资源
最近更新 更多