我有一个下拉列表,当用户在输入字段上输入内容时,我会根据用户使用过滤器输入的内容过滤列表。
我还想从过滤器对象中选择已过滤列表项的长度。怎么弄?
示例:如果您输入字母'J' - 我的长度应更新为3.因为1月,6月和7月都有字母'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(请清除输入字段并输入)