简体   繁体   English

将这个div隐藏在ng-repeat中

[英]Hide this div inside ng-repeat

I would like hide a div with values inside from an item with a button, how to do this 我想从带有按钮的项目中隐藏具有值的div,该如何执行

 <div ng-repeat="item in items"> <p>{{item.name}}</p> <div>{{item.comment}}</div> <div id="button">show comment</div> </div> 

<div ng-repeat="item in items">
    <p>{{item.name}}</p>
    <div ng-show="item.rendered">{{item.comment}}</div>
    <div ng-hide="item.rendered" ng-click="item.rendered = true">show comment</div>
    <div ng-show="item.rendered" ng-click="item.rendered = false">hide comment</div>
</div>

We make use of on-the-fly rendered attribute in your Item object (does not exist, but created within the ng-repeat and initialize it: 我们在您的Item对象中使用了即时rendered属性(不存在,但在ng-repeat创建并初始化它:

  • with false value to hide it for the first time. false以便首次将其隐藏。

or 要么

  • with true value to show it for the first time. 并具有true价值,以便首次展示。

But default it uses false 但默认情况下,它使用false

Use ngShow and, at every ngClick , change an attribute(like item.show in the example) of your item. 使用ngShow并在每个ngClick处更改项目的属性(例如item.show中的item.show )。

 var app = angular.module('myApp', []); app.controller('myController', ['$scope', function($scope){ $scope.items = [{name:'name01', comment:'comment01'}, {name:'name02', comment:'comment02'}]; }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="myController"> <div ng-repeat="item in items" > <p>{{item.name}}</p> <div ng-show="item.show">{{item.comment}}</div> <div id="button" ng-click="item.show = !item.show;">show comment</div> </div> </div> 

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

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