使用 ng-repeat 中使用的 ng-click 更改布尔值的值 [英] Changing value of a boolean using ng-click used inside a ng-repeat

查看:26
本文介绍了使用 ng-repeat 中使用的 ng-click 更改布尔值的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ng-repeat 在 div 内的 html 页面上显示一些数据.在 div 里面,我有一个按钮来分别隐藏每个 div 的内容.这是我的 html 文件的简化版本.

<div ng-repeat='x in array' ng-show="{{ x.show }}"><p>{{ x.text }}</p><button ng-click="toggle()">隐藏</button>

我的.js文件中的代码如下

var app = angular.module('task');app.controller('repeat',function($scope){$scope.array = [{显示:真实,text:'示例文本 1'},{显示:真实,text:'示例文本 2'},{显示:真实,text:'示例文本 3'}];$scope.toggle = function(){$scope.array.show = false ;};})

任何人都可以建议我进行所需的更改,以便在单击我的 div 内的按钮时,该特定 div 被隐藏.

我想我在通过 ng-click 调用 function toggle() 时引用数组的特定元素犯了一个错误

解决方案

好吧,我找到了一种真正满足我需求的方法,谢谢大家的建议.这是我实际使用的代码:

<div ng-repeat='x in array' ng-hide="x.show"><p>{{ x.text }}</p><button ng-click="toggle($index)">隐藏</button>

在我的 js 文件中我是这样使用的:

var app = angular.module('task');app.controller('repeat',function($scope){$scope.array = [{显示:真实,text:'示例文本 1'},{显示:真实,text:'示例文本 2'},{显示:真实,text:'示例文本 3'}];$scope.toggle = function(){$scope.array[index].show = false ;};})

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

解决方案

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 ;
  };
})

这篇关于使用 ng-repeat 中使用的 ng-click 更改布尔值的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆