<select multiple ng-model="comp" ng-options="c.name for c in companies | unique: 'company'" style="width: 100%; height: 200px; float: left; margin-bottom: 20px"></select>
<table>
<tr ng-repeat="c in comp">
<td>{{ c.id }}</td>
</tr>
</table>
<select multiple ng-model="dep" ng-options="c.name for c in departments | filter: {company_id: comp.id}" style="width: 100%; height: 200px; float: left; margin-bottom: 20px"></select>
<select multiple ng-model="pos" ng-options="c.name for c in positions | unique: 'position' | filter: {department_id: dep.id}" style="width: 100%; height: 200px; float: left"></select>
我的代码是什么样的,但它只能部分工作。如果我将其循环通过ng-repeat
,当我在选择框中点击公司时,c.id
打印出来就很好了。麻烦的是 - 它不会在我想过滤结果的其他选择框中打印出来。可能是什么原因?
JSFiddle: http://jsfiddle.net/9Ymvt/853/
答案 0 :(得分:0)
问题是由comp
是(选定的)公司列表而不是单个公司的事实引起的。虽然该表工作正常(ng-repeat迭代表),但过滤器失败,因为comp.id
计算结果为undefined
。
这可以通过将我们的过滤器更改为如下来解决:
<div ng-controller="fessCntrl">
<select multiple ng-model="comp" ng-options="c.name for c in companies | unique: 'company'" style="width: 100%; height: 200px; float: left; margin-bottom: 20px"></select>
<table>
<tr ng-repeat="c in comp">
<td>{{ c.id }}</td>
</tr>
</table>
<pre>{{ comp }}</pre>
<select multiple ng-model="dep" ng-options="c.name for c in departments | filter:sameId(comp, 'company_id')" style="width: 100%; height: 200px; float: left; margin-bottom: 20px"></select>
<select multiple ng-model="pos"
ng-options="c.name for c in positions | unique: 'position' | filter:sameId(dep, 'department_id')"
style="width: 100%; height: 200px; float: left"></select>
</div>
要使代码生效,必须在sameId
的范围内定义fessCntrl
。
$scope.sameId = function(grouping, object_id_field_name) {
return function(object) {
var obj_id = object[object_id_field_name];
for (var i = 0; i < grouping.length; i++) {
if (grouping[i].id == obj_id)
return true;
}
return false;
}
};