I have a static site in which a trio of side-by-side text boxes each have a button which opens a collapsible section with 100% screen width below the three text boxes. Now I’m trying to recreate that in Angular 18 using ng-bootstrap and it seems to be much harder. I think I need to configure the logic globally and I’m hoping someone might be able to point me in the direction of how to do that please.
I have this in the html template of an Angular parent component:
<div ngbAccordion class="d-flex w-100" #accordion="ngbAccordion">
<app-accordion-test
*ngFor="let accordionItem of accordionList; track accordionItem"
[accordionItem]="accordionItem"
[ngbAccordionItem]="accordionItem.id"
>
</app-accordion-test>
</div>
accordionList is an array of objects injected from a service:
[
{id: "first"},
{id: "second"},
{id: "third"}
]
And this in the corresponding child component app-accordion-test:
<div>
<h2 ngbAccordionHeader>
<button ngbAccordionButton>{{accordionItem.id}}</button>
</h2>
<div ngbAccordionCollapse>
<div ngbAccordionBody>
<ng-template>
<h2>Template test</h2>
<p>Content for the {{accordionItem.id}} </p>
</ng-template>
</div>
</div>
</div>
The above code is an experiment to see if the Accordion logic can be split between a parent and a child element, since that’s the only way I can see that I can have three text boxes in children which can then have a 100vw collapsible section appear below them. It demonstrates some of the features of the accordion but it’s glitchy. The buttons don’t work as expected in a few ways, the most problematic of which is that having shown the collapsible content, reclicking the button does not make it disappear. Trying to collapse the content produces this error in the console:
TypeError: Cannot read properties of undefined (reading ‘ngbCollapse’)
Interpreting the elements in the developer console, I’ve noticed that my attempt produces two identifiers:
ng_content_[alphanumerical string]
ng_host_[different alphanumerical string]
Whereas the model accordion code (not split between parent and child) from the ng_bootstrap site has only one:
ng_content_[alphanumerical string]
Looking at the ng-bootstrap docs: global configuration of accordions, I wonder if maybe I need to configure globally, but I don’t really have any idea what that might involve.
@Component({
selector: 'ngbd-accordion-config',
standalone: true,
imports: [NgbAccordionModule],
templateUrl: './accordion-config.html',
providers: [NgbAccordionConfig], // add the NgbAccordionConfig to the component providers
})
export class NgbdAccordionConfig {
constructor(config: NgbAccordionConfig) {
// customize default values of accordions used by this component tree
config.closeOthers = true;
}
}
Would really appreciate any pointers / advice / solutions. Thanks in advance!