1

What am i trying to do?

I send a POST request to an API and using the response I dynamically create tabs. Here's the code for the following:

<div id="wrapper" class="container-fluid">
<ngb-tabset class="nav-fill">
<ngb-tab *ngFor="let category of categories" [title]="category.name" [id]="category.id">
      <ng-template ngbTabContent>
        <!-- Here we will have some content.
    This content comes from the API which takes category.id as a param -->
      </ng-template>
    </ngb-tab>
</ngb-tabset>
</div>

I hope the code is clear.

The problem I'm facing is I'm not able to figure out how to get the content. Because I need the category.id to get the content, and somehow i have to attach a method which takes the category.id and performs the request and gets data.

So the user clicks on one tab, and some content is shown, and when he clicks on other tabs another set of content is shown which is all got through an API.

I found one method to do it. OnNgInit() I can get all the data required and then when the *ngFor runs I'll use the id reference to get the content. But I was looking for a more robust method to do it

4
  • can you share the html inside <ng-template> Commented Jan 16, 2019 at 13:29
  • @programoholic that's what I'm not able to figure out. It is loaded from a GET request based on which tab is clicked. Commented Jan 16, 2019 at 13:31
  • For simplicity assume there is only a variable binding Commented Jan 16, 2019 at 13:33
  • there is select event or focus event is there .. you can call method on such event to get the data, once done assign the value like category.content = result ; and in html {{category?.content}} Commented Jan 16, 2019 at 13:51

1 Answer 1

2

Instead of calling all content data during ngOninit() method which is sometimes never used, you can attach (ngClick) event handler to your ngb tab like this:

<ngb-tab *ngFor="let category of categories" (ngClick)="selectedTab(category.id)" [title]="category.name" [id]="category.id">
<ng-template ngbTabContent>
    <!-- Here we will have some content.
This content comes from the API which takes category.id as a param -->
  </ng-template>
</ngb-tab>

and in your component, you can call your rest method to fetch the content for the tab:

@Component({...})
export class someComponent{
/* constructors private members bla bla */

  selectedTab(id:int):any{
//call your service which makes http call to get content for your selected tab
    getContent(id);
   }
}

hope that answers your question!

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

2 Comments

for some reason ngClick event is not being fired
sorry. just got hold of its documentation. use (tabChange) method on the ngb-tabset element to call your dynamic tab content rest api. refer ng-bootstrap.github.io/#/components/tabset/examples for more information

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.