在Angular JS中过滤

时间:2016-07-19 08:27:15

标签: javascript angularjs

我有两个下拉列表用于组类型,另一个用于子组类型。

    <div class="form-group row">
        <label for="GroupType" class="col-sm-4 form-control-label">Group Type: </label>
        <div class="col-sm-8">
            <select name="selGroupType" id="selGroupType" class="form-control" ng-change="referenceAddRecord.populateGroupTypeDetails(selGroupType)" ng-options="groupType.value for groupType in referenceAddRecord.groupTypes track by groupType.id" ng-model="referenceAddRecord.groupType"></select>
        </div>
    </div>
    <div class="form-group row">
        <label for="SubGroupType" class="col-sm-4 form-control-label">Sub Group Type: </label>
        <div class="col-sm-8">
            <select name="selSubGroupType" id="selSubGroupType" class="form-control" ng-change="referenceAddRecord.populateSubGroupTypeDetails(selSubGroupType)" ng-options="subGroupType.value.split('|')[0] for subGroupType in referenceAddRecord.subGroupTypes | filter: {subGroupType.value.split('|')[1] : referenceAddRecord.groupType.id}  track by subGroupType.id" ng-model="referenceAddRecord.subGroupType"></select>
        </div>
    </div>

组类型的值类似于

{id:1,Value:"A"}
{id:2,Value:"B"}

子组类型的值如下:

{id:1,value:"Auction|1"}
{id:2,value:"Bing|1"}
{id:3,value:"TowerResearch|2"}

其中| <number>是组类型ID。

在更改组类型时,我想相应地更改子组类型下拉列表。但是

 ng-options="subGroupType.value.split('|')[0] for subGroupType in referenceAddRecord.subGroupTypes | filter: {subGroupType.value.split('|')[1] : referenceAddRecord.groupType.id}  track by subGroupType.id"

没用。

我做错了什么?

错误说Syntax Error: Token '.' is unexpected, expecting [:] at column 57 of the expression [referenceAddRecord.subGroupTypes | filter: {subGroupType.value.split('|')[1] : referenceAddRecord.groupType.id}] starting at [.value.split('|')[1] : referenceAddRecord.groupType.id}].

2 个答案:

答案 0 :(得分:1)

  

你做错了什么?

过滤器语法为filter : {field_name: value}。您没有遵循此语法

  

你能做什么

  1. 您必须为此
  2. 编写自定义过滤器
  3. 您可以遍历子组类型并向其添加组

    var subs = [{id:1,value:"Auction|1", group:1},
       {id:2,value:"Bing|1", group:1},
       {id:3,value:"TowerResearch|2", group:1}];
    subs.forEach(function(sub){
         sub.group = Number(sub.value.split('|')[1]);
    });
    
  4. 您可以使用角度过滤器

     filter: {group : referenceAddRecord.groupType.id}
    

答案 1 :(得分:0)

此外,我认为您不会将模型作为ng-click函数的参数传递:

<div class="form-group row">
    <label for="GroupType" class="col-sm-4 form-control-label">Group Type: </label>
    <div class="col-sm-8">
        <select name="selGroupType" id="selGroupType" class="form-control" ng-change="referenceAddRecord.populateGroupTypeDetails(referenceAddRecord.groupType)" ng-options="groupType.value for groupType in referenceAddRecord.groupTypes track by groupType.id" ng-model="referenceAddRecord.groupType"></select>
    </div>
</div>
<div class="form-group row">
    <label for="SubGroupType" class="col-sm-4 form-control-label">Sub Group Type: </label>
    <div class="col-sm-8">
        <select name="selSubGroupType" id="selSubGroupType" class="form-control" ng-change="referenceAddRecord.populateSubGroupTypeDetails(referenceAddRecord.subGroupType)" ng-options="subGroupType.value.split('|')[0] for subGroupType in referenceAddRecord.subGroupTypes | filter: {subGroupType.value.split('|')[1] : referenceAddRecord.groupType.id}  track by subGroupType.id" ng-model="referenceAddRecord.subGroupType"></select>
    </div>
</div>