I have successfully created a custom filter using AngularJS that displays only the fruits that begin with the letter "p." Although I believe my custom filter is implemented correctly, I have noticed that it is being called twice.
While searching for similar issues on stackoverflow, I came across a person with a similar problem. However, their problem remained unanswered and was slightly different from mine.
Check out my JSFiddle Solution here
Here is the snippet of HTML code:
<body>
<div ng-controller="ExampleCtrl" ng-app="sampleApp">
<div class="showDiffTags" ng-repeat="val in values | myFilter:'p'">{{val.name}}</div>
</div>
</body>
And here is the AngularJS code snippet:
angular.module('sampleApp', []).filter('myFilter', function() {
return function(items, firstLetter) {
var groups = [];
console.log("called function");
console.log(items.length);
for (var i = 0; i < items.length; i++) {
if (items[i].name.substring(0, 1) == firstLetter) {
groups.push(items[i]);
}
}
return groups;
}
}).controller('ExampleCtrl', function($scope) {
$scope.values = [{
name: 'apple'
}, {
name: 'banana'
}, {
name: 'orange'
}, {
name: 'avocado'
}, {
name: 'pineapple'
}, {
name: 'peach'
}, {
name: 'plum'
}, {
name: 'grapes'
}, {
name: 'mango'
}, {
name: 'papaya'
}, ];
});