gpt4 book ai didi

javascript - AngularJS:向 ng-repeat 内的图像添加切换功能

转载 作者:行者123 更新时间:2023-12-03 07:04:22 27 4
gpt4 key购买 nike

我正在使用 ng-repeat 作为 div 元素。我在 div 元素内的图像上有一个切换功能。但是,当我单击图像时,函数被触发,但它为所有 div 显示相同的数据。下面是代码。

HTML:

<div ng-repeat="item in items">
<div>
<img src="img/icon1.png" class="pull-right" ng-click="showDiv(item.itemId,$index)">
</div>
<div ng-show="hiddenDiv[$index]">
<div ng-repeat="student in studentData">
<div class="row">
<div>
<div>{{student.name}}</div>
<div>{{student.adress}}</div>
</div>
</div>
</div>
</div>
</div>

JS:

$scope.hiddenDiv=[];
$scope.showDiv=function(itemId,index) {
$scope.hiddenDiv[index] = !$scope.hiddenDiv[index];
var myResult = ListService.getList(url here);
myResult.then(function (data) {
if(data && data.list)
$scope.studentData = data.list;
});
}

在这里,我在 ng-repeat 内的所有 div 中使用相同的作用域变量。

更新答案:

js:

$scope.studentData = [];
$scope.studentData[index] = data.list;

HTML:

最佳答案

如果您希望每个项目都有独特的学生,您需要执行以下操作:

<div ng-repeat="item in items">
<div>
<img src="img/icon1.png" class="pull-right" ng-click="showDiv(item.itemId,$index)">
</div>
<div ng-show="hiddenDiv[$index]">
<div ng-repeat="student in item.studentData">
<div class="row">
<div>
<div>{{student.name}}</div>
<div>{{student.adress}}</div>
</div>
</div>
</div>
</div>
</div>

和js:

$scope.hiddenDiv=[];
$scope.showDiv=function(itemId,index) {
$scope.hiddenDiv[index] = !$scope.hiddenDiv[index];
var myResult = ListService.getList(url here);
myResult.then(function (data) {
if(data && data.list)
$scope.items[index].studentData = data.list;
});
}

或者使用与hiddenDiv相同的内容

$scope.hiddenDiv=[];
$scope.studentData = [];
$scope.showDiv=function(itemId,index) {
$scope.hiddenDiv[index] = !$scope.hiddenDiv[index];
var myResult = ListService.getList(url here);
myResult.then(function (data) {
if(data && data.list)
$scope.studentData[index].studentData = data.list;
});
}

和 html:

<div ng-repeat="student in studentData[$index]">

关于javascript - AngularJS:向 ng-repeat 内的图像添加切换功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36892253/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com