I have a search bar that lets users filter based on their entry using angular. Now I want to be able to display a message like "Nothing matches your search" when the search word entered by the user doesn't match anything in the array.
I tried monitoring a user's input using ng-change and then I loop through the array searching every parameter for the user's input but that doesn't work out. Here's a sample of my code.
html
<input ng-model="search" ng-change="check(search)">
<h4 ng-if="found === 0 ">Nothing matches your search</h4>
<div ng-controller="imCtrl" ng-repeat="imprest in imprests | filter:search">
<h2>{{imprest.name}}</h2>
</div>
controller
var office = angular.module('Office');
office.controller('imCtrl',[$scope,function('$scope'){
$scope.imprests = [
{name:'John'},
{name:'Peter'}
]
$scope.check = function (word) {
var found = 0
$scope.searching = true
for (var i = 0; i < $scope.vendorForms.length; i++) {
if($scope.imprests[i].name.includes(word){
found++
}
}
$scope.found = found
$scope.searching = true
$scope.done = true
}
})
Is it possible to know the length of the filtered array so I can display a message?