繁体   English   中英

ng-options显示所有选项的默认选项

[英]ng-options default option that displays all options

我有一个ng-options选择列表,我希望有一个显示所有选项的默认选项。 单击列表中的项目时,单击默认值时应进行过滤,它应显示所有值。 我过去可能发誓,我可以使用选项html元素(其值设置为“”)来执行此操作,但是此操作无效,有人可以帮助我解决此问题。 小提琴下面。

http://jsfiddle.net/nzfks0rq/

<select ng-model="todoFilter" ng-options="todo.name as todo.name for todo in todos" class='form-control'>
   <option value="">Choose Vendor</option>
</select>
<ul class="unstyled">
  <li ng-repeat="todo in todos | filter:{name: todoFilter}">
     {{todo.name}}
  </li>
</ul>

谢谢

@mrust是正确的,但是在您的情况下,您可以使用简单的search逻辑,因为该示例提供了

<div ng-init="friends = [{name:'John', phone:'555-1276'},
                         {name:'Mary', phone:'800-BIG-MARY'},
                         {name:'Mike', phone:'555-4321'},
                         {name:'Adam', phone:'555-5678'},
                         {name:'Julie', phone:'555-8765'},
                         {name:'Juliette', phone:'555-5678'}]"></div>

<label>Search: <input ng-model="searchText"></label>
<table id="searchTextResults">
  <tr><th>Name</th><th>Phone</th></tr>
  <tr ng-repeat="friend in friends | filter:searchText">
    <td>{{friend.name}}</td>
    <td>{{friend.phone}}</td>
  </tr>
</table>
<hr>

此代码对于了解如何使用默认过滤器按一个或多个字段组织搜索更为重要

<label>Any: <input ng-model="search.$"></label> <br>
<label>Name only <input ng-model="search.name"></label><br>
<label>Phone only <input ng-model="search.phone"></label><br>
<label>Equality <input type="checkbox" ng-model="strict"></label><br>
<table id="searchObjResults">
  <tr><th>Name</th><th>Phone</th></tr>
  <tr ng-repeat="friendObj in friends | filter:search:strict">
    <td>{{friendObj.name}}</td>
    <td>{{friendObj.phone}}</td>
  </tr>
</table>

最好的祝福

埃戈尔

您可以创建一个自定义过滤器来处理此问题。 这是上一篇文章的示例: https : //stackoverflow.com/a/27118166/643779

您的问题是您的默认选项值被解释为null所以我认为解决问题的简便方法是将watch添加到过滤器中,然后在未定义时将其转换为空字符串。

$scope.$watch('todoFilter', function(val){
  $scope.todoFilter = $scope.todoFilter? $scope.todoFilter : "";
});

小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM