1

According to the bootstrap docs you can activate a tab programatically from JavaScript; for example:

const triggerEl = document.querySelector('#myTab button[data-bs-target="#profile"]')
bootstrap.Tab.getInstance(triggerEl).show()

I want to be able to do this from an Angular component. What I have so far is this:

redirectTab(): void {
  const tab = document.querySelector(`#TabCtl button[data-bs-target="#nav-ett"]`);
  (window as any)["bootstrap"].Tab.getInstance(tab).show();
}

That (window as any)["bootstrap"] sticks out like a sore thumb; I'm sure there must be a better way to access bootstrap's APIs from Angular components.

This code doesn't work on first attempt and throws:

TypeError: Cannot read properties of null (reading 'show')

But if you click around the tabs and then try again, it works. Any ideas on a better way to do this?

Edit: ng-bootstrap is not the answer!

2 Answers 2

1

There is a completely different approach to use packages like ng-bootstrap's nav or ngx-bootstrap tabs that offer a clean and out of the box solutions.

Sign up to request clarification or add additional context in comments.

1 Comment

I meant to mention in the question that ng-bootstrap wasnt an option. It comes with it's own problems, and I find more often than not, it's a hassle to use.
0

simply triggerEl.click();

const triggerEl = document.querySelector('#myTab button[data-bs-target="#profile"]')
triggerEl.click();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.