我希望能解决三个问题......
在我的应用页面中,我有一个选择状态,另一个选择县。对于我所拥有的州:
<select ng-model="filter.stateID" ng-options="item.stateID as item.state for item in st_option">
</select>
数据:
[
{ state="California", stateID="5"},
{ state="Arizona", stateID="3"},
{ state="Oregon", stateID="38"},
{ state="Texas", stateID="44"},
{ state="Utah", stateID="45"},
{ state="Nevada", stateID="29"}
]
对于我的县选择我有:
<select ng-model="filter.countyID" ng-options="item.countyID as item.county for item in co_option">
</select>
数据:
[
{ county="Orange", countyID="191", co_state_id="5"},
{ county="Multiple Counties", countyID="3178", co_state_id="3"},
{ county="Sonoma", countyID="218", co_state_id="38"},
{ county="Los Angeles", countyID="190", co_state_id="44"}
]
这是我的ng-repeat
:
<div ng-repeat="project in projects | filter:filter">
<div>
State: {{project.state}}<br>
County: {{project.county}}<br>
<span ng-hide="{{project.stateID}}"></span>
<span ng-hide="{{project.countyID}}"></span>
</div>
</div>
因此,正如您所看到的那样,我在状态选择和县选择中使用stateID
我在县数据集中的co_state_id
中设置了相应的状态ID。
我想做一些事情:
stateID
/ co_state_id
ng-repeat
过滤stateID
,然后按countyID
过滤。我也没有办法将filter.stateID
设置为true
或按数字而不是字符串进行过滤。当我使用stateID进行过滤时,我会得到混合结果,因为有些stateID
可以包含“1”。
答案 0 :(得分:28)
通常你只想在每个帖子上提出一个问题,但我会给这三个问题一个镜头。
第1部分:为ng-show
添加filter.stateID
。由于您无法取消选择状态,因此如果您的角度为^ 1.3,则可以使用一次性绑定。
<select ng-show="::filter.stateID" ng-model="filter.countyID" ng-options="item.countyID as item.county for item in co_option">
第2部分:为{co_state_id : filter.stateID}
<select ng-show="::filter.stateID != null" ng-model="filter.countyID" ng-options="item.countyID as item.county for item in co_option | filter:{ co_state_id : filter.stateID }">
第3部分:
您正在使用模式对象进行过滤,如果id的值为1,则无关紧要:
对象:模式对象可用于过滤数组包含的对象的特定属性。例如{name:“M”,phone:“1”}谓词将返回一个项目数组,其属性名称包含“M”,属性手机包含“1”。可以使用特殊属性名称$(如{$:“text”})来接受对象的任何属性或其嵌套对象属性的匹配。这相当于如上所述与字符串匹配的简单子字符串。可以通过在字符串前加上!来取消谓词。例如{name:“!M”}谓词将返回一个属性名称不包含“M”的项目数组。
工作代码段
var app = angular.module('app', []);
app.controller('myController', function($scope) {
$scope.projects = [{
name: 'Project1',
state: 'CA',
stateID: '5',
county: 'Orange',
countyID: '191'
}, {
name: 'Project2',
state: 'CA',
stateID: '5',
county: 'LosAngeles',
countyID: '190'
}, {
name: 'Project3',
state: 'CA',
stateID: '5',
county: 'Orange',
countyID: '191'
}, {
name: 'Project4',
state: 'MadeUp',
stateID: '1',
county: 'MadeUp',
countyID: '190'
}];
$scope.st_option = [{
state: "California",
stateID: "5"
}, {
state: "Arizona",
stateID: "3"
}, {
state: "Oregon",
stateID: "38"
}, {
state: "Texas",
stateID: "44"
}, {
state: "Utah",
stateID: "45"
}, {
state: "Nevada",
stateID: "29"
}];
$scope.co_option = [{
county: "Orange",
countyID: "191",
co_state_id: "5"
}, {
county: "Multiple Counties",
countyID: "3178",
co_state_id: "3"
}, {
county: "Sonoma",
countyID: "218",
co_state_id: "38"
}, {
county: "Los Angeles",
countyID: "190",
co_state_id: "44"
}];
$scope.filter = {};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<div ng-app='app' ng-controller='myController'>
<select ng-model="filter.stateID"
ng-options="item.stateID as item.state for item in st_option"></select>
<select ng-show="::filter.stateID"
ng-model="filter.countyID"
ng-options="item.countyID as item.county for item in co_option | filter:{ co_state_id : filter.stateID }">
</select>
<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>