I'm new with angularJS and I have this data
{
"name": "test product",
"slug": "test-product",
"tags": "tag 1, tag 2, tag 3",
"price": "80.00"
}
How can I use ng-repeat and filter based on 'tags' with multiple values targeting a specific string? I tried something like
$scope.filterTags = function (item) {
return item.tags == 'tag 1' || item.tags == 'tag 2';
};
<div data-ng-repeat="product in products | filter: filterTags">
<div>{{ product.name }}</div>
</div>
I do get products with that specific tag but only when the value is starting with that specific string. Let's say products with a tag value of "tag 3, tag 2" doesn't get selected but "tag 2, tag 3" does. I want all the products with "tag 2" inside to be selected, how can I archieve this?
You need custom filter
HTML :
<div data-ng-repeat="product in products | filter: filterTags">
<div>{{ product.name }}</div>
</div>
JS :
In the controller :
arrFilterTag= ['tag1','tag2'];
.filter('filterTags', function () {
return function (products, arrFilterTag) {
products.filter(function (val) {
return arrFilterTag.indexOf(val.slug) > -1;
//arrFilterTag['tag1','tag2']
});
return products;
}
})