Answer the question
In order to leave comments, you need to log in
How to count the number of filtered values in Angular JS (filter)?
Hi everybody!
Please tell me about those who have experience in Angular JS.
The task is as follows: there is a search string for phrases and, accordingly, a list of phrases. It is necessary to count the number of found phrases and display them on the screen. For example: total - 10500, found - 399.
ng-repeat="item in main.keywords | keywordsFilter:keywordCtrl.filterModel1 | keywordsFilter:keywordCtrl.filterModel2 | keywordsFilter:keywordCtrl.filterModel3 | keywordsFilter:keywordCtrl.filterModel4 | limitTo: 300 | orderBy:'freq':true"
.filter('keywordsFilter', function () {
return function (items, criterion) {
return items.filter(function (element, index, array) {
var str = String(element.phrase);
if (str.indexOf(criterion) + 1) {
return true;
} else {
element.visible = false;
}
});
}
})
<ul class="list-inline">
<li>Всего фраз: {{main.keywords.length}}</li>
<li>Найдено: {{ ??? }}</li>
</ul>
Answer the question
In order to leave comments, you need to log in
do it in the directive, prepare the data in the controller and that's it. No need to try to make it obviously inconvenient and wrong (from an architectural point of view).
You don't want to load all 10500 (and if there are 100500) values to the front, do you? Do it on the back, and show only what you need.
PS
To get a model with filtered records, you can write something like this:
ng-repeat="item in main.filteredKeywords = (main.keywords | filter1| fliter2|....)"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question