【问题标题】:AngularJS reset filter that nothing is filteredAngularJS重置过滤器没有过滤
【发布时间】:2016-03-28 18:14:54
【问题描述】:

我在我的 ng-repeat 中使用了一个过滤器,当我导航到页面时它工作正常(所有内容都显示 - 没有过滤任何内容)并且当我过滤时一切都很好。 只有当我以在选择输入中选择 的方式“取消选择”过滤器时,我才会遇到问题。比什么都没有显示而不是全部显示。 有谁知道当我选择 时我能做到这一点,所有内容都显示在开头 - 当我导航到页面时?

somethingToIterate | filter: {institutionUserRole: {value: roleFilter}}

我的选择如下所示:

<select class="form-control roleSelect" ng-model="roleFilter" ng-options="role as role | translate for role in vm.roles">
    <option></option>
</select>

【问题讨论】:

    标签: angularjs filter


    【解决方案1】:

    如果您在 ng-model 中使用对象,我认为您需要将默认选项添加到您在 ng-repeat 中循环的列表中。
    像这样:

     $scope.options = [
       //add the default value in the options list.
       {'value':'', 'label':''},
       {'value':'1', 'label':'Option 1'},
       {'value':'2', 'label':'Option 2'},
       {'value':'3', 'label':'Option 3'},
       {'value':'4', 'label':'Option 4'},
       {'value':'5', 'label':'Option 5'},
       {'value':'6', 'label':'Option 6'}
     ];
    
    
    <div class="jumbotron">
      <select ng-model="option" ng-options="item as item.label for item in options track by item.value">
      </select>
    
      <ul>
        <li ng-repeat="item in data | filter: {name: option.value}">{{item.name}}   
        </li>
      </ul>
    </div>
    

    或者你可以这样试试,

    <select ng-model="selected.option">
      <option value='' >select</option>
      <option ng-repeat="option in options" value="option.value">{{option.label}}
      </option>
    </select>
    

    这是一个工作示例。 http://codepen.io/mkl/full/qZjwJG/

    【讨论】:

    • 您能否使用页面某处的 {{roleFilter}} 验证角色过滤器的设置。
    • before{{roleFilter}}after -> beforeafter,所以它是一个空字符串。
    • 尝试使用 ng-repeat 创建选项。
    • 如果这解决了您的问题,您是否会接受答案。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2013-09-18
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    相关资源
    最近更新 更多