gpt4 book ai didi

AngularJS - 转到上一个/下一个模式

转载 作者:行者123 更新时间:2023-12-04 11:49:26 25 4
gpt4 key购买 nike

我正在构建一个带有 angular 的应用程序,其中有一个项目列表(使用 ng-repeat),通过单击每个项目,我可以打开一个模式以查看更详细的描述。

现在为了切换到另一个模态,我必须关闭前一个,转到列表,然后单击以打开另一个模态。单击上一个/下一个按钮时,我想直接转到上一个/下一个模式。我已经举了一个我想要实现的例子。

这是html:

<div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="t in turtles">
{{t.name}} - {{t.weapon}}
<a ng-click="show = !show">Show more</a>
<div class="modal" ng-show="show">
<div class="close" ng-click="show = !show">X</div>
{{t.name}} likes to eat {{t.food}} with his {{t.weapon}}!
<div class="previous">Previous</div>
<div class="next">Next</div>
</div>
</li>
</ul>
</div>

和 Controller :
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.turtles = [
{ name: "Michellangelo", weapon: "nunchaku", food:"pizza" },
{ name: "Donatello", weapon: "bo", food:"pizza" },
{ name: "Leonardo", weapon: "katana", food:"turtle soup" },
{ name: "Rafael", weapon: "sai", food:"pizza" }
];
$scope.show=false;
});

你可以看到一个有效的 Jsfiddle here

最佳答案

这是一个有效的解决方案:http://jsfiddle.net/s7gc81zz/5/

您可以为每只海龟添加一个 show 属性,并使用 $index,而不是依赖单个变量来显示或隐藏模态。 ngRepeat 的变量来引用您在数组中的位置:

<div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="t in turtles">
{{t.name}} - {{t.weapon}}
<a ng-click="showMore(t)">Show more</a>
<div class="modal" ng-show="t.show">
<div class="close" ng-click="hideMore(t)">X</div>
{{t.name}} likes to eat {{t.food}} with his {{t.weapon}}!
<div class="previous" ng-click="showPrevious(t, $index)>Previous</div>
<div class="next" ng-click="showNext(t, $index)">Next</div>
</div>
</li>
</ul>
</div>

现在您使用范围函数来处理下一个按钮的逻辑:
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.turtles = [
{ name: "Michellangelo", weapon: "nunchaku", food:"pizza" },
{ name: "Donatello", weapon: "bo", food:"pizza" },
{ name: "Leonardo", weapon: "katana", food:"turtle soup" },
{ name: "Rafael", weapon: "sai", food:"pizza" }
];
//$scope.show=false;
$scope.showMore = function(turtle) {
turtle.show = true;
};
$scope.hideMore = function(turtle) {
turtle.show = false;
};
$scope.showNext = function(turtle, index) {
if((index+1) > ($scope.turtles.length - 1)) {
return;
}
else {
turtle.show = false;
$scope.turtles[index+1].show = true;
}

};
$scope.showPrevious = function(turtle, index) {
if((index-1) < 0) {
return;
}
else {
turtle.show = false;
$scope.turtles[index-1].show = true;
}
};
});

关于AngularJS - 转到上一个/下一个模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36629423/

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