I'm trying to filter data from a factory that can becontrolled by booleans. I can't figure out what I'm doing wrong. It's probally the syntax and myself not understanding angular right. but I've been frozen here fror a while and cannot get past. I'm showing a product list and filter it through ng-repeat directive and custom filter.
.filter('promos'[
'$scope', '$http', 'FilterData', 'ProductData', function(
$scope, $http, FilterData, ProductData){
$scope.filterData = FilterData;
$scope.products = ProductData;
angular.forEach(products, function(product){
var promoProducts = [];
//booleen filters- if all or none selected do nothing - full product list
if ($scope.filterData.topChoices[0].selected && $scope.filterData.topChoices[1].selected && $scope.filterData.topChoices[2].selected || $scope.filterData.topChoices[0].selected === false && $scope.filterData.topChoices[1].selected === false && $scope.filterData.topChoices[2].selected === false ) {}
else {
//if option selects, show corresponding products
if ($scope.filterData.topChoices[0].selected && product.onPromotion) {promoProducts.push}
if ($scope.filterData.topChoices[1].selected && product.hotSeller) {promoProducts.push}
if ($scope.filterData.topChoices[2].selected && product.freeShip) {promoProducts.push}
return promoProducts;
}
});
}])
Controller & Factories can be seen in the plunker [http://plnkr.co/edit/EDZPOPh7iMFSpPjO6AkI?p=preview][1] Here is the html:
<div ng-controller="prodCtrl">
<md-input-container class="md-icon-float md-icon-right md-block">
<label>Low Price</label>
<md-icon md-font-icon="fa fa-money"></md-icon>
<input ng-model="filterData.price.low" type="number" step="5">
<md-icon md-svg-src="img/icons/ic_euro_24px.svg"></md-icon>
</md-input-container>
<md-input-container class="md-icon-float md-icon-right md-block">
<label>High Price</label>
<md-icon md-font-icon="fa fa-money"></md-icon>
<input ng-model="filterData.price.high" type="number" step="5">
<md-icon md-svg-src="img/icons/ic_euro_24px.svg"></md-icon>
</md-input-container>
<md-checkbox style="margin-left: 15px;" class="md-secondary" ng-model="filterData.topChoices[0].selected"> Show Promotions <br><span style="font-size: 10px; color: blue;" ng-if="filterData.topChoices[0].selected">selected</span> </md-checkbox>
<md-checkbox style="margin-left: 15px;" class="md-secondary" ng-model="filterData.topChoices[1].selected"> Show Hot Sellers<br><span style="font-size: 10px; color: blue;" ng-if="filterData.topChoices[1].selected">selected</span> </md-checkbox>
<md-checkbox style="margin-left: 15px;" class="md-secondary" ng-model="filterData.topChoices[2].selected"> Show Free Shipping<br><span style="font-size: 10px; color: blue;" ng-if="filterData.topChoices[2].selected">selected</span> </md-checkbox>
</div>
<md-subheader class="md-no-sticky">Avatar with Secondary Action Icon</md-subheader>
<md-list ng-Controller="prodCtrl">
<md-list-item ng-repeat="product in products | filter:promos" ng-click class="noright">
<ul>
<h3>{{product.name}} </h3>
<p>{{product.desc}}</p>
</ul>
<ul class="md-secondary">
<li style="list-style: none;" ng-if="product.onPromotion">On Promotion!</li>
<li style="list-style: none;" ng-if="product.hotSeller">Hot-Seller!</li>
<li style="list-style: none;" ng-if="product.freeShip">Free Shipping!</li>
</ul>
<ul class="md-secondary" style="margin-right: 25px;">
<li style="list-style: none;" ng-repeat="item in product.sellers | orderBy: item.price">
{{item.seller}} ${{item.price}}</li>
</ul>
<md-button class="md-raised md-primary md-secondary">Buy</md-button>
</md-list-item>
</md-list>
Please help put me on the right direction. I'm usuing a factory because i also use the product data in a map. So I'm thinking this is the best route.
This will go inside you filter code
remove $http and $scope dependencies from the filter heres a working fiddle
var pusher = function(main, toPush) {
var alreadyPresent = false;
angular.forEach(main, function(toCheck) {
if (toCheck === toPush) {
alreadyPresent = true;
}
})
if (!alreadyPresent) {
main.push(toPush);
}
}
var p = [];
angular.forEach(products, function(product) {
if (FilterData.topChoices[0].selected &&
FilterData.topChoices[1].selected && FilterData.topChoices[2].selected || FilterData.topChoices[0].selected === false && FilterData.topChoices[1].selected === false && FilterData.topChoices[2].selected === false) {} else {
//if
if (FilterData.topChoices[0].selected && product.onPromotion) {
pusher(p, product);
}
if (FilterData.topChoices[1].selected && product.hotSeller) {
pusher(p, product);
}
if (FilterData.topChoices[2].selected && product.freeShip) {
pusher(p, product);
}
console.log('ppp')
console.log(p);
console.log('--ppp')
}
})
return p;
and to use the filter do
<md-list-item ng-repeat="product in products | promo" ng-click class="noright">