简体   繁体   English

如何在没有用户输入的情况下更新AngularJS中的筛选列表

[英]How to update the filtered list in AngularJS without the user's input

I'm using the filter and everything works nicely when a user types in the input box. 我正在使用过滤器 ,当用户输入输入框时,一切都很好用。 What I would like though is to update the filtered results through the code (for example to clear the query), without the user's input. 我想要的是通过代码更新过滤结果(例如清除查询),而无需用户的输入。 I tried to trigger the change event or simply clear the value of the input field, without any success. 我试图触发更改事件或只是清除输入字段的值,但没有任何成功。

Here is my simplest HTML 这是我最简单的HTML

<div ng-app ng-controller="test">    
    <input type="text" ng-model="query" placeholder="search..">
    <button>Clear</button>
    <ul class="unstyled">
        <li ng-repeat="item in list | filter:query">
            {{item}}
        </li>
    </ul>
</div>

And here is the JS: 这是JS:

function test($scope) {
    $scope.list = [120, 566, 777, 889, 998, 332, 112, 440, 881];

    $('button').click(function() {
        $('input').val('');
    });
}

JSFiddle 的jsfiddle

What can I do to unfilter the results without deleting the contents of the input using the keyboard, but doing that though the code (like clicking on the clear button)? 如何在不使用键盘删除输入内容的情况下取消过滤结果,但是通过代码执行此操作(例如单击清除按钮)?

Do not register your event handler via jQuery let ng-click handle that for you. 不要通过jQuery let ng-click handle为你注册你的事件处理程序。

<button ng-click="clearBox()">Clear</button>


function test($scope) {
    $scope.list = [120, 566, 777, 889, 998, 332, 112, 440, 881];

    $scope.clearBox = function(){
      $scope.query = "";
    };
}

Updated fiddle 更新了小提琴

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

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