自定义过滤Angular中的共享数据

时间:2016-08-18 02:22:15

标签: angularjs angularjs-directive angular-material angular-services angular-filters

我试图过滤可以由布尔控制的工厂的数据。我无法弄清楚我做错了什么。它的语法和我自己没有理解角度正确。但我已经被冻结了一段时间,无法过去。我正在显示产品列表并通过ng-repeat指令和自定义过滤器对其进行过滤。

.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;
       }
     });
}])

控制器&可以在plunker [http://plnkr.co/edit/EDZPOPh7iMFSpPjO6AkI?p=preview][1]中看到工厂 这是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>

请帮助我朝正确的方向前进。我正在使用工厂,因为我也在地图中使用产品数据。所以我认为这是最好的路线。

1 个答案:

答案 0 :(得分:1)

这将在您内部过滤代码

从过滤器中删除$ http和$ scope依赖项 继承人工作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;

并使用过滤器

<md-list-item ng-repeat="product in products | promo" ng-click class="noright">