Angularjs 过滤确切的数字
Angularjs filter exact number
我有一个表格显示州和县的下拉 select 框,一切正常,除非我选择一个只有一个数字的州,说数字“1”过滤器将包括其他也有数字“1”的州。我需要精确过滤或 "true" 但不确定代码应该如何查找,我仍在学习 Angular.
这是我的 Javascript:
var app = angular.module('app', []);
app.controller('myController', function($scope) {
$scope.projects = [{
name: 'First thing',
state: 'CA',
stateID: '1',
county: 'Orange',
countyID: '191'
}, {
name: 'Another Thing',
state: 'CA',
stateID: '1',
county: 'LosAngeles',
countyID: '190'
}, {
name: 'In California',
state: 'CA',
stateID: '1',
county: 'Orange',
countyID: '191'
}, {
name: 'Hey Arizona!',
state: 'Arizona',
stateID: '13',
county: 'Multiple Counties',
countyID: '3178'
},{
name: 'hello Utah',
state: 'Utah',
stateID: '14',
county: 'Utah County',
countyID: '200'
}];
$scope.st_option = [{
state: "California",
stateID: "1"
}, {
state: "Arizona",
stateID: "13"
},{
state: "Utah",
stateID: "14"
}];
$scope.co_option = [{
county: "Orange",
countyID: "191",
co_state_id: "1"
}, {
county: "Multiple Counties",
countyID: "3178",
co_state_id: "13"
}, {
county: "Sonoma",
countyID: "218",
co_state_id: "1"
}, {
county: "Los Angeles",
countyID: "190",
co_state_id: "1"
}, {
county: "Utah County",
countyID: "200",
co_state_id: "14"
}];
$scope.filter = {};
$scope.clearFilter = function() {
$scope.filter = {};
};
});
和视图:
<div ng-app='app' ng-controller='myController'>
<h3>Records: {{(projects|filter:filter).length}} of {{projects.length}}</h3>
<p><input type="text" name="generalSearch" placeholder="Search Project Title" ng-model="filter.name"></p>
<p><select ng-model="filter.stateID"
ng-options="item.stateID as item.state for item in st_option"></select>
<select ng-model="filter.countyID"
ng-options="item.countyID as item.county for item in co_option | filter:{ co_state_id : filter.stateID }">
</select></p>
<p><a href="" id="clear-filter" ng-click="clearFilter()">Reset Filters</a></p>
<div ng-repeat="project in projects | filter:filter">
<div>
<br>Name: {{ project.name }}
<br>State: {{project.state}}
<br>County: {{project.county}}
<br>
<span ng-hide="{{project.stateID}} "></span>
<span ng-hide="{{project.countyID}} "></span>
</div>
</div>
</div>
还有一个fiddle:
将 filter
的最后一个参数设置为 true,请参阅 comparator parameter:
<div ng-repeat="project in projects | filter:filter:true">
编辑以允许多个过滤器:
<input type="text" name="generalSearch" placeholder="Search Project Title" ng-model="filter2.name">
<div ng-repeat="project in projects | filter:filter:true | filter:filter2">
我有一个表格显示州和县的下拉 select 框,一切正常,除非我选择一个只有一个数字的州,说数字“1”过滤器将包括其他也有数字“1”的州。我需要精确过滤或 "true" 但不确定代码应该如何查找,我仍在学习 Angular.
这是我的 Javascript:
var app = angular.module('app', []);
app.controller('myController', function($scope) {
$scope.projects = [{
name: 'First thing',
state: 'CA',
stateID: '1',
county: 'Orange',
countyID: '191'
}, {
name: 'Another Thing',
state: 'CA',
stateID: '1',
county: 'LosAngeles',
countyID: '190'
}, {
name: 'In California',
state: 'CA',
stateID: '1',
county: 'Orange',
countyID: '191'
}, {
name: 'Hey Arizona!',
state: 'Arizona',
stateID: '13',
county: 'Multiple Counties',
countyID: '3178'
},{
name: 'hello Utah',
state: 'Utah',
stateID: '14',
county: 'Utah County',
countyID: '200'
}];
$scope.st_option = [{
state: "California",
stateID: "1"
}, {
state: "Arizona",
stateID: "13"
},{
state: "Utah",
stateID: "14"
}];
$scope.co_option = [{
county: "Orange",
countyID: "191",
co_state_id: "1"
}, {
county: "Multiple Counties",
countyID: "3178",
co_state_id: "13"
}, {
county: "Sonoma",
countyID: "218",
co_state_id: "1"
}, {
county: "Los Angeles",
countyID: "190",
co_state_id: "1"
}, {
county: "Utah County",
countyID: "200",
co_state_id: "14"
}];
$scope.filter = {};
$scope.clearFilter = function() {
$scope.filter = {};
};
});
和视图:
<div ng-app='app' ng-controller='myController'>
<h3>Records: {{(projects|filter:filter).length}} of {{projects.length}}</h3>
<p><input type="text" name="generalSearch" placeholder="Search Project Title" ng-model="filter.name"></p>
<p><select ng-model="filter.stateID"
ng-options="item.stateID as item.state for item in st_option"></select>
<select ng-model="filter.countyID"
ng-options="item.countyID as item.county for item in co_option | filter:{ co_state_id : filter.stateID }">
</select></p>
<p><a href="" id="clear-filter" ng-click="clearFilter()">Reset Filters</a></p>
<div ng-repeat="project in projects | filter:filter">
<div>
<br>Name: {{ project.name }}
<br>State: {{project.state}}
<br>County: {{project.county}}
<br>
<span ng-hide="{{project.stateID}} "></span>
<span ng-hide="{{project.countyID}} "></span>
</div>
</div>
</div>
还有一个fiddle:
将 filter
的最后一个参数设置为 true,请参阅 comparator parameter:
<div ng-repeat="project in projects | filter:filter:true">
编辑以允许多个过滤器:
<input type="text" name="generalSearch" placeholder="Search Project Title" ng-model="filter2.name">
<div ng-repeat="project in projects | filter:filter:true | filter:filter2">