简体   繁体   English

隐藏Angular中数组中的非匹配元素

[英]Hide non matching elements from array in Angular

I have a search box with a ng-model assigned to it: 我有一个分配了ng模型的搜索框:

<input type="text" class="form-control" placeholder="Search" ng-model="searchLibrary.text">

And a ng-repeat with a filter searchLibrary.text 并使用过滤器searchLibrary.text进行ng-repeat

<div ng-repeat="w in items | filter:searchLibrary.text" on-item-removed="onItemRemoved(item)">

So, when the user enters something, the filter removes all non-matching elements from the array, but is there a way to hide non-matching elements instead of removing them? 因此,当用户输入内容时,过滤器会从数组中删除所有不匹配的元素,但有没有办法隐藏不匹配的元素而不是删除它们?

The reason why removing elements is problematic is I have a callback method assigned to the ng-repeat which gets called when a item is removed but it gets triggered when a user searches for some item which isn't the correct behaviour. 删除元素有问题的原因是我有一个分配给ng-repeat的回调方法,当项目被删除时会被调用,但是当用户搜索某个不正确行为的项目时会触发它。

Edit : All the elements in the items array are draggable, so the user can manually drag and drop items from panel A to panel B. The callback is triggered when an item gets removed, but it shouldn't get triggered when user searches for a item description. 编辑items数组中的所有元素都是可拖动的,因此用户可以手动将项目从面板A拖放到面板B.当项目被删除时触发回调,但是当用户搜索项目时不应该触发回调。商品描述。

Any help is much appreciated. 任何帮助深表感谢。

You can use ng-if or ng-show to hide elements. 您可以使用ng-ifng-show隐藏元素。 You could replace 你可以替换

<div ng-repeat="w in items | filter:searchLibrary.text" on-item-removed="onItemRemoved(item)">

with

<div ng-repeat="w in items" ng-if="w === searchLibrary.text" on-item-removed="onItemRemoved(item)">

Here's a link to an example on CodePen https://codepen.io/anon/pen/VmPzMz 这是CodePen上的示例链接https://codepen.io/anon/pen/VmPzMz

You could also use an ng-class. 您也可以使用ng-class。

.is-hidden {
    display: none;
}

<div ng-repeat="w in items" ng-class="{'is-hidden': w===searchLibrary.text}"
    on item-removed="onItemRemoved(item)">

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

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