Angular 中的自定义筛选共享数据
Custom Filtering Shared Data in Angular
我正在尝试从可以由布尔值控制的工厂中过滤数据。我不知道我做错了什么。这可能是语法和我自己不理解 angular 的原因。但我已经在这里被冻结了一段时间,无法通过。我正在显示一个产品列表并通过 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>
请帮我指明正确的方向。我正在使用工厂,因为我也在地图中使用产品数据。所以我认为这是最好的路线。
这将进入您的过滤器代码
从过滤器中删除 $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">
我正在尝试从可以由布尔值控制的工厂中过滤数据。我不知道我做错了什么。这可能是语法和我自己不理解 angular 的原因。但我已经在这里被冻结了一段时间,无法通过。我正在显示一个产品列表并通过 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>
请帮我指明正确的方向。我正在使用工厂,因为我也在地图中使用产品数据。所以我认为这是最好的路线。
这将进入您的过滤器代码
从过滤器中删除 $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">