Obasi Obeny Oj Obasi Obeny Oj - 2 months ago 9
AngularJS Question

How to ng-repeat to display depending on starting letter of item values

I have an array with items names ranging from A-Z.
What I want to do is ng-repeat for each letter of the alphabet.

For A letters in Array:

<div ng-repeat="item in items | orderBy: 'item_name'">
{{item.item_name}}
</div>


For B letters in Array:

<div ng-repeat="item in items | orderBy: 'item_name'">
{{item.item_name}}
</div>


How can I achieve this?

Answer

You can achieve this with custom filter

<div ng-repeat="item in items | filter:criteriaMatch()">
   {{item.name}}
</div>

and filter function like below

$scope.criteriaMatch = function( ) {
    return function( item ) {
        return item.name.startsWith("z");
    };
};

here is Working fiddle

hope it helps you :)

Comments