I have a landing page that implements an accordion. The number of groups in the accordion are managed by a list of objects defining the properties(title, active... etc.) Each panel of the accordion contains a specific sub component. I would like to define the groups as such:
@Component({
selector: 'aa-home',
template: require('./home.component.html'),
styles: [require('./home.component.scss')],
directives: [ACCORDION_DIRECTIVES, QUOTE_COMPONENTS]
})
export class HomeComponent implements OnInit {
public oneAtATime: boolean = true;
public groups: AccordianGroup[] = [];
constructor() {
this.groups.push(new AccordianGroup(true, 'Quote', 'quote', '<aa-quote (response)="onContinue($event)"></aa-quote>'));
this.groups.push(new AccordianGroup(false, 'Introduction', 'intro', '<aa-intro (response)="onContinue($event)"></aa-intro>'));
this.groups.push(new AccordianGroup(false, 'Applicant Information', 'applicant', '<aa-applicant (response)="onContinue($event)"></aa-applicant>'));
this.groups.push(new AccordianGroup(false, 'Details', 'details', '<aa-details(response)="onContinue($event)"></aa-details>'));
this.groups.push(new AccordianGroup(false, 'Review', 'review', '<aa-review (response)="onContinue($event)"></aa-review>'));
this.groups.push(new AccordianGroup(false, 'Payment', 'payment', '<aa-payment (response)="onContinue($event)"></aa-payment>'));
}
ngOnInit() {
console.log(_.capitalize('starting home'));
}
onContinue(message): void {
let index = _.findIndex(this.groups, function(x) { return x.name === message.componentName; });
if (index < this.groups.length - 1) {
this.groups[index].isActive = false;
this.groups[index + 1].isActive = true;
}
}
toggleActive(group): void {
this.groups.forEach(function(x) { x.isActive = false; });
group.isActive = !group.isActive;
}
}
class AccordianGroup {
constructor(
public isActive: boolean,
public title: string,
public name: string,
public template: string
) { }
}
The HTML like this:
<div *ngFor="let group of groups">
<div class="accordion" (click)=toggleActive(group)>{{group.title}}</div>
<div class="panel" [ngClass]="{show: group.isActive}" [innerHTML]="group.template"></div>
</div>
This doesn't work... nothing is rendered. It feels as if I'm going about this the wrong way but I'm not able to sort this out. The idea behind this is that depending on the user... I need to filter some groups out and really... not have a dozen or more groups in the html that have the exact same code except for the one line where the sub component is placed. Is this even possible?