Changing value of a boolean using ng-click used inside a ng-repeat

喜夏-厌秋 提交于 2019-12-21 20:25:30

问题


I am displaying some data on my html page inside a div using ng-repeat. Inside the div I have a button in order to hide the content of each div seperately.Here is a simplified version of my html file.

<body ng-app="task" ng-controller="repeat">
    <div ng-repeat='x in array' ng-show="{{ x.show }}">
      <p>{{ x.text }}
      </p>
  <button ng-click="toggle()">Hide</button>
    </div>
</body>

the code in my .js file is as follows

var app = angular.module('task');
app.controller('repeat',function($scope){
    $scope.array = [{
        show: true,
        text:'Sample Text 1'},
      { 
        show: true,
        text:'Sample Text 2'},
      { 
        show: true,
        text:'Sample Text 3'}];

    $scope.toggle = function(){
       $scope.array.show = false ;
      };
})

Can anyone suggest me the required changes so that on clicking the button inside my div , that particular div gets hidden.

I think I am committing a mistake in referencing the particular element of the array while calling the function toggle() through ng-click


回答1:


Put your element as an argument in toggle function.

<button ng-click="toggle(x)">Hide</button>

and change it in controller like this:

$scope.toggle = function(x){
    x.show = !x.show;
};



回答2:


And easy to achieve this without calling the function in the controller:

<body ng-app="task" ng-controller="repeat">
  <div ng-repeat='x in array' ng-show="showDetail">
    <p>{{ x.text }}</p>
      <button ng-click="showDetail != showDetail">Hide</button>
  </div>
</body>

The method above will also hide the button if you click hide. If you want to hide your content and show it again, the following code can achieve that:

<body ng-app="task" ng-controller="repeat>
  <div ng-repeat='x in array'>
    <div class="content" ng-show="showContent">
      <p>{{ x.text }}</p>
    </div>
    <div class='btn btn-control'>
      <button ng-click="showContent != showContent"> Hide </button>
    </div>
  </div>
</body>



回答3:


Well I found a way to actually cater my needs , thank you all for your suggestions. Here is the code I actually used:

<body ng-app="task" ng-controller="repeat">
 <div ng-repeat='x in array' ng-hide="x.show">
  <p>{{ x.text }}
  </p>
  <button ng-click="toggle($index)">Hide</button>
 </div>
</body>

and in my js file I used it like this:

var app = angular.module('task');
app.controller('repeat',function($scope){
 $scope.array = [{
    show: true,
    text:'Sample Text 1'},
  { 
    show: true,
    text:'Sample Text 2'},
  { 
    show: true,
    text:'Sample Text 3'}];

 $scope.toggle = function(){
   $scope.array[index].show = false ;
  };
})



回答4:


In your html pass

   <button ng-click="toggle(x.$index)">Hide</button>

While in js

  $scope.toggle = function(index){
  $scope.array[index].show = !$scope.array[index].show;
  };


来源:https://stackoverflow.com/questions/31073190/changing-value-of-a-boolean-using-ng-click-used-inside-a-ng-repeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!