使用For循环,If语句和ng-repeat显示数组项(json)

时间:2016-11-22 19:19:20

标签: javascript angularjs arrays json ionic-framework

我正在尝试过滤两个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>

1 个答案:

答案 0 :(得分:2)

我认为你错过了==符号  if($ scope.category_limit =='Jewellery'){ ..... }