避免筛选异步 ng 选项默认值

Avoid filter on asynchronous ng-options default value

本文关键字:选项 默认值 ng 异步 筛选      更新时间:2023-09-26

angularjs 1.3.6的更新破坏了我的一些过滤器。

我试图修复它们并获得此解决方案。

隐藏默认选项:

<select ng-model="search.type" ng-options="g.id as g.name for g in group">
    <option ng-show="false" value="">Any</option>
</select>

添加不会过滤列表的"任何"选项。

$scope.group = [];
$scope.group[0] = {id:'',name:'Any'};

没有人有更好的解决方案来做到这一点?使用这个 ng-show 技巧对我来说似乎不太好。

你几乎做对了所有事情。缺少 2 件事,然后您不需要ng-show黑客:

  1. 使<select>的 ViewModel 属性成为实际的筛选对象,而不仅仅是 Id 属性,并相应地更改筛选器字符串。
  2. 使 ViewModel 属性等于"Any"对象 - 这会将<select>设置为正确的选项。

1)

<div ng-repeat="item in items| filter:search.type">
    {{item.name}}
</div>
<select ng-model="search" ng-options="g as g.name for g in group">
</select>

2)

$scope.search = $scope.group[0] = {id:'',name:'Any'};

更新的 jsFiddle