简体   繁体   English

AngularJS使用NG重复显示/隐藏切换

[英]AngularJS Show/Hide Toggle with NG-Repeat

I have a list of items which on click should open the single item, but currently when trying to open the single item it opens all items and on second click closed all the items - could somebody please advice where I am going wrong with my code below. 我有一个项目列表,点击它应该打开单个项目,但目前当试图打开单个项目时,它打开所有项目,然后在第二次点击关闭所有项目 - 有人可以建议我在下面的代码我出错了。 Thank you. 谢谢。

HTML HTML

<div data-ng-repeat="item in items" layout="column">
  <div layout="row">
    <md-button class="md-primary" ng-click="toggleFilter()">Item {{$index + 1}}</md-button>
  </div>
  <div ng-hide="toggle">
    <!-- Content -->
  </div>
</div>

JS JS

$scope.toggle = true;
$scope.toggleFilter = function() {
  $scope.toggle = $scope.toggle === false ? true : false;
};

Edit Your code as following: 编辑您的代码如下:

HTML HTML

<div data-ng-repeat="item in items" layout="column">
  <div layout="row">
    <md-button class="md-primary" ng-click="toggleFilter(item)">Item {{$index + 1}}</md-button>
  </div>
  <div ng-hide="item.toggle">
    <!-- Content -->
  </div>
</div>

JS JS

$scope.toggleFilter = function(item) {
  item.toggle = !item.toggle;
};

Hope that works :) 希望有效:)

ng-repeat creates a new scope for each item. ng-repeat为每个项目创建一个新范围。 Each new scope will inherit toggleFilter and toggle from its parent. 每个新范围都将继承toggleFilter并从其父级toggle Right now you are switching toggle state for parent scope. 现在您正在切换父范围的切换状态。 Thus all child scopes get the same value. 因此,所有子范围都获得相同的值。 If you want to toggle value on child scope simply use this instead of $scope . 如果要在子范围上切换值,只需使用this而不是$scope Demo 演示

$scope.toggleFilter = function() {
  this.toggle = !this.toggle
}

Add a parametr inside toggleFilter, and make $scope.toggle an array. 在toggleFilter中添加一个参数,并使$ scope.toggle成为一个数组。 Like that : 像那样 :

HTML HTML

<div data-ng-repeat="item in items" layout="column">
  <div layout="row">
    <md-button class="md-primary" ng-click="toggleFilter($index)">Item {{$index + 1}}</md-button>
  </div>
  <div ng-hide="toggle[$index]">
    <!-- Content -->
  </div>
</div>

JS JS

$scope.toggle = [];
$scope.toggleFilter = function(inx) {
  $scope.toggle[inx] = $scope.toggle[inx] === false ? true : false;

}; };

hi, so i hope i understood it correctly, but below is a sample code based on your question above. 嗨,所以我希望我理解正确,但下面是一个基于上述问题的示例代码。 Have taken the liberty to go ahead and change your code a bit. 已经冒昧地继续改变您的代码。

HTML HTML

<div data-ng-repeat="item in items" layout="column">
<div layout="row">
<md-button class="md-primary" ng-click="toggleFilter()">{{buttonText}} </md-button>
</div>
<div ng-show="toggle">
<!-- Content -->
</div>
</div>

Your JS will be as follows 你的JS将如下

$scope.toggle= false; $ scope.toggle = false;

$scope.buttonText = "Show";
$scope.toggleFilter = function(){
$scope.toggle = !$scope.toggle;
$scope.buttonText = "Show";
if(toggle===true){
$scope.buttonText = "Hide";
}
};

I hope this helped. 我希望这有帮助。

your HTML will be as follows ( only change in HTML is required ). 您的HTML将如下所示(仅需要更改HTML)。

<div ng-show=showMe>
<!--content goes here -->
<ul>
<li ng-hide = "showMe">item1</li>
<li ng-hide = "showMe">item2</li>
<li ng-hide = "showMe">item3</li>
<li ng-hide = "">item4</li>
</ul>
</div>

I hope your question is answered. 我希望你的问题得到解答。 If not please leave a comment i will be really happy to help. 如果没有,请发表评论,我将非常乐意提供帮助。

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

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