如何使用Angular 2创建简单的手风琴? [英] How can I create simple accordion with Angular 2?
本文介绍了如何使用Angular 2创建简单的手风琴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
之前,我使用这个简单的脚本来创建简单的手风琴
Before, I use this simple script to create a simple accordion
(function() {
$('dl.accordion').on('click', 'dt', function() {
this_= $(this);
this_
.addClass("selected")
.next()
.slideDown(200)
.siblings('dd')
.slideUp(200);
this_.siblings()
.removeClass("selected")
});
})();
此html
<dl class="accordion">
<dt>What are your hours?</dt>
<dd>We are open 24/7.</dd>
<dt>What are your hours?</dt>
<dd>We are open 24/7.</dd>
</dl>
现在,我想为用Angular 2编写的这段代码创建一个副本。
Now I want to create a copy of this code written in Angular 2.
如何在Angular 2中创建一个像上面的简单手风琴?
How can I create a simple acordion like above in Angular 2?
我想我必须学习渲染器,elementRef等。
您能建议我应该学习创建此主题的其他主题吗?
I guess I have to learn renderer, elementRef etc. Could you suggest other topics which I should learn to create this?
推荐答案
尝试此解决方案,这是非常简单的手风琴:
try this solution, this is very simple accordion:
应用程序/手风琴。 component.ts
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'tp-accordion',
template: `
<h2 class="accordion-head" (click)="onClick($event)">{{ title }}</h2>
<div class="accordion-body" [class.active]="active">
<ng-content></ng-content>
</div>
`,
styles: [
`
.accordion-head {
cursor: pointer;
}
.accordion-body {
display: none;
}
.accordion-body.active {
display: block;
-webkit-animation: fadeIn .3s;
animation: fadeIn .3s;
}
@-webkit-keyframes fadeIn {
from { opacity: 0; transform: scale(0); }
to { opacity: 1; transform: scale(1); }
}
@keyframes fadeIn {
from { opacity: 0; transform: scale(0); }
to { opacity: 1; transform: scale(1); }
}
`
],
})
export class Accordion {
@Input() title: string;
@Input() active: boolean = false;
@Output() toggleAccordion: EventEmitter<boolean> = new EventEmitter();
constructor() {}
onClick(event) {
event.preventDefault();
this.toggleAccordion.emit(this.active);
}
}
应用/手风琴- group.component.ts
import { Component, ContentChildren, QueryList, AfterContentInit, OnDestroy } from '@angular/core';
import { Accordion } from './accordion.component';
@Component({
selector: 'tp-accordion-group',
template: `
<ng-content></ng-content>
`
})
export class AccordionGroup {
@ContentChildren(Accordion) accordions: QueryList<Accordion>;
private subscriptions = [];
private _accordions = [];
constructor() {}
ngAfterContentInit() {
this._accordions = this.accordions;
this.removeSubscriptions();
this.addSubscriptions();
this.accordions.changes.subscribe(rex => {
this._accordions = rex;
this.removeSubscriptions();
this.addSubscriptions();
});
}
addSubscriptions() {
this._accordions.forEach(a => {
let subscription = a.toggleAccordion.subscribe(e => {
this.toogleAccordion(a);
});
this.subscriptions.push(subscription);
});
}
removeSubscriptions() {
this.subscriptions.forEach(sub => {
sub.unsubscribe();
});
}
toogleAccordion(accordion) {
if (!accordion.active) {
this.accordions.forEach(a => a.active = false);
}
// set active accordion
accordion.active = !accordion.active;
}
ngOnDestroy() {
this.removeSubscriptions();
}
}
应用/应用。 component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { PostsService } from './posts.service';
@Component({
selector: 'app-root',
template: `
<tp-accordion-group>
<tp-accordion *ngFor="let post of posts" [title]="post.title">
{{ post.body }}
</tp-accordion>
</tp-accordion-group>
`
})
export class AppComponent implements OnInit, OnDestroy {
posts = [];
private subscription: any;
constructor(private postsSvc: PostsService) {}
ngOnInit() {
this.subscription = this.postsSvc.getPosts().subscribe(res => {
if (res.length) {
this.posts = res.slice(0, 10);
}
})
}
ngOnDestroy() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
}
app / posts.service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class PostsService {
postsUrl: 'https://jsonplaceholder.typicode.com/posts';
constructor(private http: Http) {
}
getPosts() {
return this.http.get(this.postsUrl)
.map(res => {
let body = res.json();
return body || [];
})
.catch(console.log);
}
}
在线演示: https://plnkr.co/edit/xFBllK?p=preview
文档:
- EventEmitter
- @ContentChildren
- QueryList
这篇关于如何使用Angular 2创建简单的手风琴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文