[英]angularjs ng-repeat hide element with a click event
There is a list of items which is displayed using ng-repeat
. 有一个使用
ng-repeat
显示的项目列表。 Each item is associated with a hide link. 每个项目都与一个隐藏链接关联。 The intent is to hide an item if its corresponding hide link is clicked.
目的是如果单击项目的相应隐藏链接,则将其隐藏。
view: 视图:
<div ng-repeat="item in products">
<div>{{ item }}</div>
<a href"javascript:void(0)" ng-click="hideMe(item)">[delete]</label>
</div>
How could I implement the function hideMe(item)
such a way that it could hide item div element , something like following, ng-if could identify when to hide based on the click event - 我该如何实现函数
hideMe(item)
这样一种方式,使其可以隐藏item div元素 ,类似以下内容,ng-if可以根据click事件确定何时隐藏-
<div ng-if="...">{{ item }}</div>
You could use an array of objects like this: $scope.products = [{name: 'hello', status: true}]
您可以使用以下对象数组:
$scope.products = [{name: 'hello', status: true}]
And then you can hide them changing the status
property: 然后您可以隐藏它们,更改
status
属性:
<div ng-repeat="item in products">
<div ng-show="item.status">
{{ item.name }} <a href"javascript:void(0)" ng-click="item.status = false">[delete]</label>
</div>
</div>
For every list-item, you want to hide it if it's clicked. 对于每个列表项,如果单击它,您都希望将其隐藏。 The best way we can do this is by adding the
ng-hide
directive. 我们最好的方法是添加
ng-hide
指令。
Using the ng-click
directive on a button, we can set the hidden
property of an item to true, meaning it should be hidden. 在按钮上使用
ng-click
指令,我们可以将项目的hidden
属性设置为true,这意味着它应该被隐藏。
<ul>
<li ng-repeat="fruit in fruits" ng-hide="fruit.hidden">
<p>{{fruit.name}}</p>
<button ng-click="hideMe(fruit)">hide li</button>
</li>
</ul>
$scope.hideMe = function (fruit) {
fruit.hidden=true;
alert('hide this li');
};
Here is a fiddle 这是一个小提琴
http://jsfiddle.net/5yh8bxay/1/ http://jsfiddle.net/5yh8bxay/1/
You can use $index to do that. 您可以使用$ index做到这一点。
Something like this. 这样的事情。
<div ng-repeat="item in products">
<div ng-hide="selected.index === $index">{{ item }}</div>
<a href"javascript:void(0)" ng-click="selected.index = $index">[delete]</label>
</div>
Just store the selected value when clicked and use-hide you can use ng-class to hide the item, comparing them to the selected index. 只需在单击时存储选定的值,然后使用隐藏即可使用ng-class隐藏该项目,并将其与选定的索引进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.