仅在按钮单击时进行角度过滤

时间:2016-03-24 16:24:13

标签: javascript angularjs filter

我似乎无法让我的过滤器仅在按钮点击时执行,它会在第一次执行,然后根据框中的任何文本自动过滤。有人可以帮助我吗?

<section class="searchField">
        <input type="text" ng-model="searchTerm">
        <button type="button" ng-click="search()">Search</button>
</section>

<section ng-show="!resultsHidden">
    <div class="no-recipes" ng-hide="players.length">No results</div>
    <article class="playerSearch" ng-repeat="player in players | filter: filter" ng-cloak>
        <h2>
            <a href="#/players/{{player._id}}">{{player.name}}</a>
        </h2>
    </article>
</section>

使用Javascript:

$scope.myFilter = function (player) {
            var found = [];
            for(player in players) {
                if (player.name === $scope.searchTerm || player.number === $scope.searchTerm){
                    found.push(player);
                }
            }
            return found;
        };

        function search(){
            $scope.filter = $scope.searched;
            $scope.resultsHidden = false;
        }

        $scope.searched = function(player){
            if (!$scope.searchTerm || (player.number == $scope.searchTerm || (player.name.toLowerCase().indexOf($scope.searchTerm.toLowerCase()) != -1))){
                return true;
            }
            return false;
        };

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var app = angular.module("app",[])
app.controller('ctrl',['$scope', function($scope){
      $scope.resultsHidden = false;
$scope.players = [

{"name": "alpha"},
{"name": "beta"},
{"name": "gama"}
];   
}]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  
  <section class="searchField">
        <input type="text" ng-model="searchTerm">
        <button type="button" ng-click="filter = searchTerm;resultsHidden = !resultsHidden">Search</button>
</section>

<section ng-show="resultsHidden">
    <div class="no-recipes" ng-hide="players.length">No results</div>
    <article class="playerSearch" ng-repeat="player in players  | filter:{name:filter}" ng-cloak>
        <h2>
            <a href="#/players/{{player._id}}">{{player.name}}</a>
        </h2>
    </article>
</section>
</div>
&#13;
&#13;
&#13;

试试这个

   <button type="button" ng-click=" filter = searchTerm">Search</button>

答案 1 :(得分:1)

首先,您应该像这样创建过滤器:

angular.module('myApp')
.filter('my-filter', function( /* dependencies goes here */ ){
    return function (input, isFilterActive) {
        if (isFilterActive){
            // filter your data
            // your list will go into 'input' variable
            // return filtered list
        }
        return input; //return data unfiltered;
    }

});

你的HTML应该是这样的:

<p ng-repeat="x in subcategories | my-filter:isFilterActive">
  {{x.text}}
</p>

其中'isFilterActive'变量可以放在$ scope上。如果'isFilterActive'为true,则会过滤您的列表。 您可以创建一个更改其值的按钮。

您还可以查看角度过滤器以获取更多详细信息或本教程:https://scotch.io/tutorials/building-custom-angularjs-filters

玩得开心。