angular ngrepeat startWith filter

today i had some fun trying to make a "startWith" filter for ng-repeat.

simple start with

the most simple solution is to modify the default angular filter, just this line inside the filter function, deep inside the angular.js file there is our function "function filterFilter()", find this line

return ('' + obj).toLowerCase().indexOf(text) > -1;

and you only need to change it to

return ('' + obj).toLowerCase().indexOf(text) === 0;

example : http://plnkr.co/edit/iRzIj77N9rAoiCSMbRyk?p=preview


any word start with

but then they asked that if we have "In God I Really Do Trust" that 't' will bring it, i.e. make a filter that will find if any word start with, so I made a little nice filter, it even supports object to 1 level depth

http://plnkr.co/edit/xFhy7CdHnAZcEvgDA59L?p=preview


maybe there is an OOTB way to do it

while working on all that I noticed that the default filter comes with a comparator, so we can just use that.
the only downside here is that you must provide the scope with the comparator, so its not and individual thing, see example

http://plnkr.co/edit/xFhy7CdHnAZcEvgDA59L?p=preview

on the other hand you can always put it in a service and inject it, I still think its the best thing to do.

Comments

Popular posts from this blog

c# Service Play Sound with NAudio example by Moshe

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem

JS/JQ simulate Enter event