Angularjs' in'在ng-options中过滤

时间:2015-10-13 11:32:22

标签: javascript angularjs

我的选择框是ng-repeat as,

<tr ng-repeat="project in projects | filter:filter">
    <td>
        <select class="form-control" ng-model="project.roleIds" ng-options= "role.id as role.name for role in roleList " multiple></select>
    </td>
<td>
    <select class="form-control" ng-model="project.company" ng-options= "obj.id as obj.name for obj in compnayList | filter:{roleId:project.roleIds}"></select>
    </td>
</tr>

我的roleId选择是多个,所以我想过滤选择角色的公司列表下拉列表。因此,如果我选择两个角色,那么拥有该角色的公司将在下拉列表中出现。 现在,如果我只选择一个角色,那么它将被它过滤,但不会被多个角色过滤。我可以通过角度滤波器在ng-options中执行此操作。自定义过滤器可以使用吗?

$scope.companyList=[{
    id:1,
    name:"ABC",
    roleId:1
},
{
    id:2,
    name:"ABCd",
    roleId:2
},
{
    id:3,
    name:"ABCgh",
    roleId:1
}];


$scope.roleList=[{
    id:1,
    name:"Grade A",
},
{
    id:2,
    name:"Grade B",
},
{
    id:3,
    name:"Grade C",
}];

项目列表是这样的,

$scope.projects=[{
    id:100,
    projectName:"Project 1", 
    roleIds: [1,2],
    company:1
},
....
];

2 个答案:

答案 0 :(得分:0)

点击此处查看如何使用过滤器http://dojo.telerik.com/EmoQu

 <input type='text' ng-model='roleId' placeholder='role id'>
 <select class="form-control" ng-model="project.available" ng-options= 'obj.id as obj.name for obj in [{id:1,name:"ABC",roleId:1},     {id:2,name:"ABCd",roleId:2},{id:3,name:"ABCgh",roleId:1}] | filter:roleId'
  

考虑你的项目阵列就在那里。

答案 1 :(得分:0)

使用comparator

$scope.in = function (items, value) {
    return items.indexOf(value) !== -1;
};

在html中:

<select class="form-control" ng-model="project.available"
ng-options="obj.id as obj.name for obj in compnayList | filter:{roleId:project.roleIds}:in">
</select>