我很少有链接和来自ng repeat的div。当我单击任何链接时,它将去/滚动到相同id的相同div。这很好,但是在这里我需要基于id扩展div。我单击任何链接,只有那些div应该扩展其他div才仍然折叠。.soon。我尝试了基于id进行操作,但无法正常工作,有人可以帮我提供下面的代码,以及关于plunker的更新演示 https://plnkr.co/edit/MRZgVHYMWsff022Mq3dV?p=preview
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.4/angular-filter.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-sanitize.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js'></script>
<script src="script.js"></script>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css'>
<body ng-app="app">
<div ng-controller="AccordionDemoCtrl">
<div class="my-fixed-header">
<a href="index.html#/#anchor{{x}}" ng-click="gotoDiv(x)" ng-repeat="x in [1,2,3,4,5]">
Go to Div {{x}}
</a>
</div>
<div class="scroll-div">
<div style="border:1px solid;" id="anchor{{group.id}}" ng-repeat="group in groups">
<div class="parents" ng-click="open(group)"><i ng-class="{'glyphicon-minus': group.isOpen, 'glyphicon-plus': !group.isOpen}"></i> {{ group.title }}
</div>
{{ group.content }}
<ul class="childs" ng-show="group.isOpen">
<li ng-repeat="item in group.list">
<span ng-bind-html="item"></span>
</li>
</ul>
</div>
</div>
</div>
</body>
<style type="text/css">
.scroll-div {
height: 200px;
overflow: scroll;
margin-top: 10px;
}
.anchor {
border: 2px dashed red;
padding: 10px 10px 200px 10px;
}
.my-fixed-header {
}
.my-fixed-header > a {
display: inline-block;
margin: 5px 15px;
}
</style>
var app=angular.module('app', ['ui.bootstrap','ngSanitize','angular.filter']);
app.controller('AccordionDemoCtrl', function ($scope,$location,$anchorScroll) {
$scope.oneAtATime = true;
$scope.open = function (item) {
$scope.groups.filter(a=> a ===item).forEach(a=>{
a.isOpen = !a.isOpen;
});
$scope.closeOthers(item);
}
$scope.closeOthers = function (item) {
$scope.groups.filter(a=> a !==item).forEach(a=>{
a.isOpen = false;
});
}
$scope.gotoDiv = function(x) {
if(x){
$scope.groups.forEach(a=>{
a.isOpen = true;
});
}
else{
a.isOpen = false;
}
};
$scope.groups = [
{
title: 'title 1',
id:'1',
list: ['<i>item1a</i> blah blah',
'item2a',
'item3a',
'item4a',
'item5a',
'item6a',
'item7a'
]
},
{
title: 'title 2',
id:'2',
list: ['item1b',
'<b>item2b </b> blah ',
'item3b']
},
{
title: 'title 3',
id:'3',
list: ['item1c',
'<b>item2c </b> blah ',
'sss3c']
},
{
title: 'title 4',
id:'4',
list: ['<i>item1a</i> blah blah',
'item2a',
'item3a',
'item4a',
'item5a',
'item6a',
'item7a'
]
},
{
title: 'title 5',
id:'5',
list: ['<i>item1a</i> blah blah',
'item2a',
'item3a',
'item4a',
'item5a',
'item6a',
'item7a'
]
}
];
$scope.groups[0].isOpen = true;
});
答案 0 :(得分:0)
要实现此目的,您可以再添加1个条件(if (a.id === x.toString())
)来检查gotoDiv函数中的选定div,如下所示:
$scope.gotoDiv = function (x) {
if (x) {
$scope.groups.forEach(a = > {
if (a.id === x.toString()) {
a.isOpen = true;
}
});
}
else {
a.isOpen = false;
}
};