简体   繁体   English

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

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

I have a ng-options pick list that I want there to be a default option that displays all options; 我有一个ng-options选择列表,我希望有一个显示所有选项的默认选项。 When you click an item in the list it should filter when you click on the default value it should show all values. 单击列表中的项目时,单击默认值时应进行过滤,它应显示所有值。 I could have sworn in the past I was able to do this using an option html element with a value set to "", but this is not working, can someone help me work this out. 我过去可能发誓,我可以使用选项html元素(其值设置为“”)来执行此操作,但是此操作无效,有人可以帮助我解决此问题。 Fiddle below. 小提琴下面。

http://jsfiddle.net/nzfks0rq/ 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>

thanks 谢谢

The @mrust is right, but in your case you can just use simple search logic as that example provide @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>

this code more important to understand how organize search by one or multiple fields using default filter 此代码对于了解如何使用默认过滤器按一个或多个字段组织搜索更为重要

<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>

Best regards 最好的祝福

Egor 埃戈尔

You could create a custom filter to handle this. 您可以创建一个自定义过滤器来处理此问题。 Here's an example from a previous post: https://stackoverflow.com/a/27118166/643779 这是上一篇文章的示例: https : //stackoverflow.com/a/27118166/643779

Your problem is that your default options value is interpreted as null So in my opinion the easies way to solve your issue is just add watch to your filter and convert it to empty string when it's not defined. 您的问题是您的默认选项值被解释为null所以我认为解决问题的简便方法是将watch添加到过滤器中,然后在未定义时将其转换为空字符串。

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

Fiddle 小提琴

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

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