我想隐藏其他人的详细信息并显示当前点击的名称的详细信息
http://jsbin.com/tewegahobi/edit?html,js,output
<li ng-click="showDetail = true" ng-repeat="item in items">{{item.name}}
<span ng-show="showDetail == true">{{item.detail}}</span>
</li>
不确定我做得对吗,我可以单击以显示详细信息,但当我单击特定名称时,它不会隐藏其他人的详细信息。
最佳答案
您可以通过使用 scope
监视单击的项目来一次仅显示一个名称。然后,您还可以使用 scope
定义与 ng-click
一起使用的函数,以便当用户单击某个项目时,值会发生变化。每个项目都有其 ng-show
属性,仅在与用户之前选择的项目匹配时才显示该项目。
function TodoCrtl($scope) {
$scope.items = [{name:"James",detail:"something of James"},{name:"John",detail:"something of John"}]
// Holds the mame of the item clicked by the user.
$scope.chosen = '';
// This function is activated each time the user clicks on an element
// with 'ng-click' that is associated with this funciton.
$scope.setChosen = function(itemName) {
$scope.chosen = itemName;
}
}
<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<meta charset=utf-8 />
<title>ng-click</title>
</head>
<body>
<div ng-controller="TodoCrtl">
<li ng-click="setChosen(item.name)" ng-repeat="item in items">{{item.name}}
<span ng-show="item.name == chosen">{{item.detail}}</span>
</li>
</div>
</body>
</html>
关于javascript - 使用纯 angularjs 的 Accordion ,无需 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39361946/