我有一個搜索欄,讓用戶可以使用角度基於其輸入進行過濾。現在,當用戶輸入的搜索詞與數組中的任何內容不匹配時,現在我希望能夠顯示「Nothing與您的搜索相匹配」的消息。如何知道角度的過濾陣列的長度
我試着用ng-change來監視用戶的輸入,然後循環遍歷數組搜索用戶輸入的每個參數,但是這並不奏效。這是我的代碼示例。
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>
控制器
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
}
})
是否有可能知道過濾數組的長度因此可以顯示一個消息?
您可以檢查這個答案 - https://stackoverflow.com/questions/15316363/how-to-display-length-of-filtered-ng-repea叔數據?RQ = 1 –