Iは4つの要素のアコーディオンは、各要素は、私が最初の要素をクリックして、代わりに表示するとき、表示されるコンテンツをhiddes有します最初の要素の内容を上げると、もう一方の要素も表示されます。
期待される動作:私は最初の要素をクリックして、その要素に属するコンテンツを表示し、hiddingに他のコンテンツを保存しておきたい
。
コード:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-sabias-que',
templateUrl: './sabias-que.component.html',
styleUrls: ['./sabias-que.component.scss']
})
export class SabiasQueComponent implements OnInit {
private _isOpen : boolean = false;
private tips : Array<any> = [
{
heading: 'Title 1',
content: 'Content to be displayed'
},
{
heading: 'Title 1',
content: 'Content to be displayed'
},
{
heading: 'Title 1',
content: 'Content to be displayed'
},
{
heading: 'Title 1',
content: 'Content to be displayed'
}
]
closeOthers(openGroup): void {
this.tips.forEach((tip) => {
if (tip !== openGroup) {
tip.isOpen = false;
}
});
}
set isOpen(value: boolean) {
debugger;
this._isOpen = value;
if (value) {
this.closeOthers(this);
}
}
get isOpen() {
return this._isOpen;
}
constructor() { }
ngOnInit() {
}
showContent(): void {
this.isOpen = !this.isOpen;
}
}
HTML:
<ul class="tips-list">
<li *ngFor="let tip of tips">
<h3 class="tips-list__title"
[ngClass]="{'tips-list__title--active' : isOpen}" (click)="showContent()">
{{ tip.heading }}
</h3>
<p class="tips-list__answer" [hidden]="!isOpen">
{{ tip.content }}
</p>
</li>
</ul>
してください誰かが提供し、私は、コードやコンセプトの説明をいただければ幸いです答えは、私はjQueryのか、バニラJSではなくので、これを行う方法を知っていればそれはOOPでした私は'this'
の使用を全く理解していません。
非常に明確でコードを手に入れました。ありがとうございました。 –
次のhttp://angularjs.blogspot.co.uk/2016/04/5-rookie-mistakes-to-avoid-with-angular.htmlを読むことをおすすめします。 – 72GM