【问题标题】:angularjs filter ng-options by options already selectedangularjs 按已选择的选项过滤 ng-options
【发布时间】:2019-01-08 00:01:37
【问题描述】:

所以,我有一个基于我收到的 JSON 的md-select。这些选项有一个“类型”属性,我可以按如下所示进行过滤:

$scope.data = [
    {name:"1-1",Id:1,type:1},
    {name:"2-2",Id:2,type:2}, 
    {name:"3-2",Id:3,type:2},
    {name:"4-2",Id:4,type:2},
    {name:"5-3",Id:5,type:3},
    {name:"6-3",Id:6,type:3}
];
<md-select multiple class="form-control" ng-model="selectedIds"> 
    <md-option ng-value="item.Id" ng-repeat=""item as (item.Name) for item in data">
        {{item.Name}}">
    </md-option>
</select>

但我需要的是使用已在同一下拉列表中选择的选项过滤选择中显示的选项。因此,如果我选择选项“3-2”,下拉菜单将仅显示“2-2、3-2 和 4-2”。我怎样才能做到这一点?

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    可以使用this other question 找到解决方案: 在更改时,我使用所选 Id 的过滤器获取所选类型,然后将该类型用作选项的过滤器。此外,如果未选择任何选项,则“selectedType”应设置为空,否则将按第一个选项的类型进行过滤。

    <md-select multiple class="form-control" ng-model="selectedIds"
    ng-change="selectedType=(selectedIds.length=0)?'':(data|filter:{Id:selectedIds})[0].type"> 
        <md-option ng-value="item.Id" ng-repeat="item in data | filter : selectedType">
            {{item.Name}}">
        </md-option>
    </select>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 2015-11-04
      相关资源
      最近更新 更多