Problem:
I am facing an issue with my accordion containing 4 elements. When I click on the first element, it displays not only its content but also the content of the other 3 elements.
Expected Solution:
I want to be able to click on a specific element and have only that element's content displayed while hiding the content of the other elements.
Code Snippet:
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'
},
...
]
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 Code:
<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>
If you can provide an explanation or code example, it would help me understand how to resolve this issue. I am familiar with jQuery and vanilla JS, but struggling with the concept of using 'this'
.