I am pretty new to angular. I am trying to add a filter parameter to my controller that will filter a search by every letter in a word, in order of letter. So imagine my data contained the following words: horses, oranges, terminator, Motorola, floral, orthopedic. When I searched for "or" in my search engine, all words would appear in the result because they all contain "or." Instead, I'd want a filter that filters my data in strict order of letter so I would only get "oranges" and "orthopedic"
This is my current filter in my controller:
$scope.activateFilter = function(textBoxValue) {
$scope.$apply(function() {
if ( textBoxValue=="") {
$scope.pages = FacebookPage.userPages;
} else {
$scope.pages=$filter('filter')($scope.pages,{name:textBoxValue});
}
});
};
This is the HTML for the filtering in case it helps:
<div id="searchEngineController" ng-controller="SearchEngineController">
<ul class="rig">
<li ng-repeat="page in pages">
<a ng-click="pageClick($index)">
<img src= "{{page.image}}" class="img-responsive img-circle" height="140" width="240">
<p style="font-size:25px; text-align:center">{{page.name}}<p>
</a>
</li>
</ul>
</div>
And my JS function:
pagesSearchBar.pageValueDidChange = function(value) {
angular.element(document.getElementById('searchEngineController')).scope().activateFilter(value);
};
Thanks!
You are re-inventing the wheel. Checkout angular build-in filter for ng-repeat
https://docs.angularjs.org/api/ng/directive/ngRepeat
Reading your code more thoroughly I think you have some misunderstanding of angular logic there as well. For example, you don't need extra function to detect search change and then getting the scope from dom. You could simply do:
Something like that
UPDATE
So If I understand correctly now, you will want to display only one result for a filter. You could achieve that by somehow combining the filter with limitTo (no time to fiddle around now)
Or even as a temporary solution use css:
UPDATE 2
Ok, now I understand, so you want to use strict filtering in the way that only items starting with the search phrase show up. Angular is ready for that too, check below snippet
Comparator reference: https://docs.angularjs.org/api/ng/filter/filter