简体   繁体   English

简单的搜索过滤器在ng-if angularjs中不起作用

[英]Simple search filter is not working in ng-if angularjs

I'm using angularjs search filter, it's working fine but I want search box only in Cars list so I added ng-if="key=='Cars'" on search box so it will appear in cars only, but after adding ng-if filter stopped working, I'm not able to figureout why it isn't working? 我正在使用angularjs搜索过滤器,但工作正常,但我只希望在“汽车”列表中使用搜索框,因此我在搜索框中添加了ng-if="key=='Cars'" ,因此它将仅出现在汽车中,但在添加ng-if过滤器停止工作,我无法弄清为什么它不工作?

<div class="form-group" ng-if="key=='Cars'">
 <input type="text" ng-model="search" placeholder="Search in {{key}}" class="form-control input-sm" />
</div>

 angular.module('myApp', []).controller('myCtrl', function($scope) { $scope.data = { "Cars": ["Ferrari", "Lamborghini", "Mercedes", "BMW", "Audi", "Bugatti", "Ford"], "object": [0, 1, 2, 3, 4, 5, 6, 7], "object3": [0, 1, 2, 3, 4, 5, 6, 7] } }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <div class="container"> <div class="row"> <div class="col-xs-4" ng-repeat="(key,val) in data"> <div class="panel panel-default"> <div class="panel-heading">{{key}}</div> <div class="panel-body"> <div class="form-group" ng-if="key=='Cars'"> <input type="text" ng-model="search" placeholder="Search in {{key}}" class="form-control input-sm" /> </div> <table class="table"> <tr ng-repeat="j in val | filter:search"> <td>{{j}}</td> </tr> </table> </div> </div> </div> </div> </div> </div> 

There isn't any error in console 控制台中没有任何错误

Change your ng-if to ng-show="key==='Cars'" 将您的ng-if更改为ng-show="key==='Cars'"

Here is a working Fiddle 这是一个工作的小提琴

You can use this Fiddle as a solution. 您可以使用此小提琴作为解决方案。 Maintain an array of search params respective to key. 维护与key有关的一系列搜索参数。 What is does is, it maintains a search param for each of the keys in your data object. 它的作用是,它为数据对象中的每个键维护一个搜索参数。 It creates a new array called search in your data . 它在您的data创建一个称为search的新数组。 And $index is the index of the parameter being used. $index是所使用参数的索引。 For cars, it is 0, for object it would be 1 and so on. 对于汽车,它是0,对于对象,它是1,依此类推。 So, the textbox for cars will only update the corresponding search parameter bound to that, ie the value at 0th index. 因此,汽车文本框将仅更新绑定到该文本框的相应搜索参数,即第0个索引处的值。 The other inputs, that are hidden are actually bound to values as 1st and 2nd index respectively. 其他隐藏的输入实际上分别绑定到作为第一索引和第二索引的值。 So there is no override of value. 因此,没有价值的替代。

If you don't want search model to be global, then wrap your table inside ng-if 如果您不希望search模型是全局的,则将表包装在ng-if

 angular.module('myApp', []).controller('myCtrl', function($scope) { $scope.data = { "Cars": ["Ferrari", "Lamborghini", "Mercedes", "BMW", "Audi", "Bugatti", "Ford"], "object": [0, 1, 2, 3, 4, 5, 6, 7], "object3": [0, 1, 2, 3, 4, 5, 6, 7] } }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <div class="container"> <div class="row"> <div class="col-xs-4" ng-repeat="(key,val) in data"> <div class="panel panel-default"> <div class="panel-heading">{{key}}</div> <div class="panel-body"> <div class="form-group" ng-if="key=='Cars' || key=='object3'"> <input type="text" ng-model="search" placeholder="Search in {{key}}" class="form-control input-sm" /> <table class="table"> <tr ng-repeat="j in val | filter:search"> <td>{{j}}</td> </tr> </table> </div> <div class="form-group" ng-if="key!='Cars' && key!='object3'"> <table class="table"> <tr ng-repeat="j in val | filter:search"> <td>{{j}}</td> </tr> </table> </div> </div> </div> </div> </div> </div> </div> 

I had the same issue when filtering a list from a dropdown that displayed with ng-if. 从ng-if显示的下拉列表中过滤列表时,我遇到了同样的问题。 The problem is that ng-if creates a child scope, so in order to reach the list you need change to "ng-model="$parent.search" 问题是ng-if创建了一个子作用域,因此为了到达列表,您需要更改为“ ng-model =” $ parent.search“

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

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