我正在动态创建div列表。我想要的内容只有拳头 div将默认显示。但是当用户点击第二个div时 第二个内容应该显示,第一个内容是隐藏。依此类推第三个和第四个。
这就像手风琴。这是我的代码
<div class="content-area">
<div class="container-fluid">
<div class="accordian">
<div class="errors-cont" *ngFor="#error of errors">
<div class="acc-header active" (click)="onClick()">
<div class="image"><img src="image2/nike.png"></div>
<div class="text">
<div class="title">Nike<span>20-04-2016, 03:10 PM</span></div>
<p>{{error.title}}</p>
</div>
</div>
<div class="acc-desc" [class.hide]="isSpecial">
<div class="image"></div>
<div class="text">
<p>{{error.desc}}</p>
<div class="acc-block">
<div class="title">ERROR TYPE asdf</div>
<p>Lorem ipsum dolor sit ame</p>
</div>
<div class="acc-block">
<div class="title">REASON</div>
<p>Lorem ipsum dolor sit ame</p>
</div>
<div class="acc-block">
<div class="btns">
<input name="" type="button" value="RESOLVE" class="transparent">
<input name="" type="button" value="IGNORE" class="green">
</div>
</div>
</div>
</div>
</div><!-- end or ngFor loop -->
</div>
</div>
</div>
import {Component} from '@angular/core';
import {HeroService} from './error.service';
import { ROUTER_DIRECTIVES } from '@angular/router';
import {CORE_DIRECTIVES} from '@angular/common';
import {DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/ng2-bootstrap';
@Component({
moduleId: module.id,
selector: 'errors-view',
templateUrl: 'errors-view.html',
providers: [HeroService],
styleUrls: ['errors.css'],
directives: [DROPDOWN_DIRECTIVES, CORE_DIRECTIVES, ROUTER_DIRECTIVES]
})
export class ErrorsViewComponent {
errors:string[] = [];
isClose = true;
isActive= false;
isSpecial= false;
//disValue = true
constructor(private heroService:HeroService) {
heroService.loadItems()
.subscribe(data => this.errors = data);
console.log('erros will be shown in this page');
}
togglePopover() {
this.isClose = !this.isClose;
}
onClick(){
this.isActive = true;
this.isSpecial = true;
//this.disValue = false;
console.log("active false");
//var x = document.getElementsByClassName("acc-header");
console.log(x.length);
// for (var i=0; i < x.length; i++) {
// x[i].onclick = function() {deleteIt(this)}
// }
}
}
答案 0 :(得分:2)
PS: - 作为替代
如果你想使用手风琴,为什么不使用它呢?
http://www.primefaces.org/primeng/#/accordion
PrimeNg为我们提供了所有需要添加此功能的功能
import {Accordion} from 'primeng/primeng';
import {AccordionTab} from 'primeng/primeng';
答案 1 :(得分:2)
如果是第一个,请使用索引添加“有效”类。
*ngFor="#error of errors; let i=index"
div class="acc-header" [class.active]="i == 0" (click)="onClick(i)"