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

标签 javascript angularjs arrays json ionic-framework

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

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

Angular/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>

最佳答案

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

关于javascript - 使用 For 循环、If 语句和 ng-repeat 显示数组项(json),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749973/

相关文章:

javascript - 输入字段(类型 ="number")一旦其内容发生更改,就会丢失 angularjs 的双向 'BIND' 属性。

java - 在java中选择 boolean 数组中的随机元素

javascript - Express.js,我新添加的路由出现 404(未找到)错误

javascript - variable === constant 和 constant === variable 有什么区别

javascript - 吉森忽视了我的一条规则

javascript - 如何在 javascript 中将 DateTime 字符串从使用斜杠格式化为使用连字符?

javascript - 如何使用 Angular JS 拦截每个 Ajax 请求

javascript - Uncaught Error : [$injector:modulerr] http://errors. angularjs.org/1.4.8/$injector/modulerr?p0=myApp&p1=Error

arrays - select 仅返回 XML 中的一项

java - 缺少变量