我使用 ng-repeat 在 div 内的 html 页面上显示一些数据。在 - 的里面div
我有一个按钮来隐藏每个内容的内容div
单独。这是我的 html 文件的简化版本。
<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 文件中的代码如下
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 被隐藏。
我认为我在调用时引用数组的特定元素时犯了一个错误function toggle()
通过 ng-click
将您的元素作为切换函数中的参数。
<button ng-click="toggle(x)">Hide</button>
并在控制器中更改它,如下所示:
$scope.toggle = function(x){
x.show = !x.show;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)