我有这个 fiddle HERE
我想检查模型“sectionSelect”的选定值,并根据选定的值,无论它是“1”还是“2”,我想用与所选值匹配的新值填充newArr,以便相应地过滤最低和最高价格。
我现在正在做的是......
if ($scope.sectionSelect == 1) {
for($i=0; $i < arr.length; $i++){
if(arr[$i]['section'] === 1) {
newArr.push(arr[$i]['value']);
}
}
} else if($scope.sectionSelect == 2) {
for($i=0; $i < arr.length; $i++){
if(arr[$i]['section'] === 2) {
newArr.push(arr[$i]['value']);
}
}
}
但这似乎不起作用。 我该怎么办?
这就是我所拥有的并且它正在工作 http://jsfiddle.net/sheko_elanteko/Anj3w/26/ 我只是想为其添加过滤功能。
最佳答案
使用 Angular 过滤内容的最简单方法是使用内置的过滤功能。我在这里更新了你的 fiddle :http://jsfiddle.net/CQyTa/
对 html 的更改使用下拉列表来设置所选内容的范围值。然后,这些值可以用作过滤器(在本例中,我根据选择的部分过滤最小值和最大值)。
<select ng-model="sectionSelect" ng-options="section.label for section in sections" ng-change="clearMinMax()">
</select>
<select ng-model="minVal" ng-options="item.value for item in app_data | filter:sectionSelect.section | orderBy:'value'">
<option value="">Min Price</option>
</select>
<select ng-model="maxVal" ng-options="item.value for item in app_data | filter: sectionSelect.section | orderBy:'-value'">
<option value="">Max Price</option>
</select>
根据在这些下拉列表中选择的内容,您可以使用所有这些来过滤应显示的内容。 minMax 函数位于 Controller 中,它只是检查值是否在 min 和 max 之间。
<div ng-repeat="item in app_data | filter:sectionSelect.section | filter:minMax | orderBy:'value'">
{{item.value}}
</div>
如果您有任何疑问,请告诉我!
关于javascript - 使用 angularjs 观察下拉列表的选定值以填充数组取决于选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24431750/