问题描述
我正在使用 ng-repeat 在 div 内的 html 页面上显示一些数据.在 div
里面,我有一个按钮来分别隐藏每个 div
的内容.这是我的 html 文件的简化版本.
我的.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()
时引用数组的特定元素犯了一个错误
好吧,我找到了一种真正满足我需求的方法,谢谢大家的建议.这是我实际使用的代码:
在我的 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 更改布尔值的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!