使用布尔值改变NG单击NG重复内部使用 [英] Changing value of a boolean using ng-click used inside a ng-repeat

查看:121
本文介绍了使用布尔值改变NG单击NG重复内部使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是我的html页面上使用NG-重复显示的div里面的一些数据。在 DIV 我为了隐藏每个内容有一个按钮 DIV seperately.Here是一个简化版本我的HTML文件。

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>

在我的.js文件中的code是如下:

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

任何人都可以给我建议,以便进行必要的更改在单击里面我 DIV ,特定的div获取隐藏的按钮。

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

我想我在引用数组中的特定元素,同时调用功能切换()犯了一个错误通过NG-点击

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

推荐答案

好吧,我发现了一种真正满足我的需求,谢谢大家的建议。这里是code我实际使用:

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>

在我的js文件我用它是这样的:

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单击NG重复内部使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆