gpt4 book ai didi

javascript - ng-repeat 中的 ng-show ng-hide 如何单击/触发每个包装 block

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

如何单击隐藏/显示那些包含在 li 中的 div/类,现在如果我单击第一个项目,两个项目都会显示。有没有像 jQuery 这样的方法可以在 Angular 中检查 this

在线示例:http://jsfiddle.net/qp0x1zcc/

 <div ng-app="editer" ng-controller="myCtrl" class="container">
<ul ng-repeat="item in items">
<li ng-click="show = !show" ng-init='show = false'>
<span ng-hide="show">{{item.name}}</span>
<form ng-show="show">
<input ng-model="item.name">
</form>
</li>
<li ng-click="show = !show">
<span ng-hide="show">{{item.d}}</span>
<form ng-show="show">
<input ng-model="item.d">
</form>
</li>
</ul>
</div>

最佳答案

尝试这样。简单明了。

var editer = angular.module('editer', []);
function myCtrl($scope) {
$scope.index = -1;
$scope.index2 = -1;
$scope.items = [{
name: "item #1",
d: "names 1"
}, {
name: "item #2",
d: "names 2"
}, {
name: "item #3",
d: "names 3"
}];
$scope.setIndex = function(item){
$scope.index = $scope.items.indexOf(item);
$scope.index2 = -1;

}
$scope.setIndex2 = function(item){
$scope.index = -1;
$scope.index2 = $scope.items.indexOf(item);

}

$scope.clearIndex = function(){
$scope.index = -1;
$scope.index2 = -1;
}

}
.container {
margin-top: 10px;
font-family: arial;
}

.container header {
padding-bottom: 20px;
border-bottom: 1px solid black;
}

ul,
input,
.container {
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="editer" ng-controller="myCtrl" class="container">


<ul ng-repeat="item in items">

<li ng-click="setIndex(item)" ng-dblClick = "clearIndex()">
<span ng-show="index != $index">{{item.name}}</span>
<form ng-show="index == $index">
<input ng-model="item.name">
</form>
</li>
<li ng-click="setIndex2(item)" ng-dblClick = "clearIndex()">
<span ng-show="index2 != $index">{{item.d}}</span>
<form ng-show="index2 == $index">
<input ng-model="item.d">
</form>
</li>
</ul>
</div>

关于javascript - ng-repeat 中的 ng-show ng-hide 如何单击/触发每个包装 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36637817/

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