eohdev eohdev - 12 days ago 5
JSON Question

Display array items(json) using For Loop, If Statement and ng-repeat

I am trying to filter through two JSON array objects in order to display selected results from the arrays. I am using two For Loops and an If Statement to filter out what needs to be shown.

However my issue comes in that the If Statement does not seem to filter out the results of the For Loop. Every item in the JSON object is displayed. Please help.

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>

Answer

i think your missing the == sign if ($scope.category_limit == 'Jewellery'){ ..... }

Comments