2015-11-19 36 views
0

我有一個下拉列表,當用戶在輸入字段上輸入時,我正在根據用戶過濾列表,他使用過濾器鍵入的內容。如何獲得過濾器對象的匹配長度?

以及我想從過濾器對象中選擇過濾列表項的長度。如何獲得?

例如:如果你輸入字母「J」 - 我的長度應更新爲3月因爲,六月和七月都有字母「J」

這裏是我的代碼:

var app = angular.module('plunker', []); 

app.controller('MainCtrl', function($scope) { 
    $scope.months = [ 
       "January", "February", "March", 
       "April", "May", "June", "July", 
       "August", "September", "October", 
       "November", "December" 
      ]; 

    $scope.defaultMonth = 'Select a Month'; 

    $scope.showList = false; 

    $scope.setMonth = function (month) { 
    $scope.defaultMonth = month; 
    $scope.showList=false; 
    } 

}); 

這裏是HTML:

<form name="monthForm"> 
     <h2>Total Match : 0 </h2> 
     <input 
     type="text" 
     name="selectMonth" 
     id="selectMonth" 
     ng-model="defaultMonth" 
     ng-focus="showList=true" 
     ng-pattern="{{ month }}" 
     required> 
     <span ng-show="monthForm.selectMonth.$error.required">Please enter something!</span> 
     <div> 
     <ul ng-show="showList" ng-onmouseover="showList=true"> 
      <li ng-repeat="month in months | filter:defaultMonth" ng-click="setMonth(month)">{{month}}</li> 
     </ul> 
     </div> 
     <input type="button" ng-disabled="monthForm.$invalid" value="Submit"> 
    </form> 

Live Demo(請清除輸入字段和鍵入)

回答

1

保持過濾列表中的變量

像這樣

ng-repeat="month in filtered=(months | filter:defaultMonth)" 

然後打印該變量的長度

像這樣

{{filtered.length}} 

PLUNKR

+0

我可以通過該長度至控制器,以及? – 3gwebtrain

+0

yap。你可以在'$ scope.filtered'上找到它@ 3gwebtrain –

+0

如何在過濾器中添加區分大小寫? – 3gwebtrain

1
<h2>Total Match : {{(months|filter:defaultMonth).length}} </h2> 

Live demo