/ / jednoduché filtrovanie objektov s určitou vlastnosťou celočíselná hodnota angular - javascript, angularjs, angularjs-ng-repeat, angularjs-filter

jednoduché filtrovanie objektov s určitou vlastnosťou celočíselná hodnota angular - javascript, angularjs, angularjs-ng-repeat, angularjs-filter

Chcem odfiltrovať všetky objekty s množstvommenej ako 1 a musel som vytvoriť vlastný filter, aby som to dosiahol. Dúfala som, že k tomuto problému existuje jednoduchšie riešenie ako vlastný filter.

Nasledujú nie práca:

<tr ng-repeat="product in products | filter: {quantity: "!0"}">

To bude filtrovať 10,20 a tak ďalej okrem 0.

Nakoniec som použil tento vlastný filter:

app.filter("quantityFilter", function() {
return function( products) {
var filtered = [];
angular.forEach(products, function(product) {
if(product.quantity > 0 ){
filtered.push(product);
}
});
return filtered;
};
});

HTML:

<tr ng-repeat="product in products | quantityFilter">

Existuje na to riešenie hladšie? Páči sa mi to (nefunguje):

<tr ng-repeat="product in products | filter: {quantity: >0}">

odpovede:

2 pre odpoveď č. 1

Namiesto písania samostatného filtra môžete použiť funkciu predikátov pre filter. Toto je detailne opísané v jazyku AngularJS dokumentácia.

tak pre vás toto sa stáva:

<tr ng-repeat="product in products | filter:noZeroQuantity">

a v regulátore:

$scope.noZeroQuantity = function(value, index, array) {
return value.quantity !== 0;
}

Napísal som príklad jsfiddle.