5

I am very new to angular 6, I have created tabs in HTML and I need to convert it in angular 6 and also make it dynamic. below is my HTML code-

<ul class="nav nav-tabs side_nav" role="tablist">
  <li role="presentation" class="dashboard_li active">
     <a href="#one" class="coin_name" aria-controls="one" role="tab" data-toggle="tab"></a>
  </li>
  <li role="presentation" class="dashboard_li">
    <a href="#two" class="coin_name" aria-controls="two" role="tab" data-toggle="tab"></a>
  </li>
</ul>
<div class="tab-content">
  <div role="tabpanel" class="tab-pane active" id="one"></div>
  <div role="tabpanel" class="tab-pane active" id="two"></div>
</div>

I have converted it angular 6, below is my angular code -

<ul class="nav nav-tabs side_nav" role="tablist">
  <li *ngFor="let wallet of coinwallet; let first = first;" [ngClass]="{firstItem: first}" role="presentation" class="accounts_li" [class.active]="selectedwallet===wallet">
    <a (click)="selectedwallet = coinwallet" aria-controls="one" role="tab" data-toggle="tab"></a>
  </li>
</ul>

<div class="tab-content">
  <div role="tabpanel" class="tab-pane active" id="one"></div>
  <div role="tabpanel" class="tab-pane active" id="two"></div>
</div>

Now I have two pints here - 1- how would I make tab-content dynamic according to tabs 2- Currently tabs are not working, as I am changing from one tab to another tab, tab-content always remain same, it's not changing according to the tab. Any help would be appreciated. Thanks in advance.

2 Answers 2

10

you need to show/hide tab-content using *ngIf angular directive

<ul class="nav nav-tabs side_nav" role="tablist">
  <li *ngFor="let wallet of coinwallet; let first = first;" [ngClass]="{firstItem: first}" role="presentation" class="accounts_li" [class.active]="selectedwallet===wallet">
    <a (click)="selectedwallet = wallet" aria-controls="one" role="tab" data-toggle="tab">{{wallet}}</a>
  </li>
</ul>

<div class="tab-content">
  <div *ngIf="selectedwallet === coinwallet[0]" role="tabpanel" class="tab-pane active" id="one">wallet 1 content</div>
  <div *ngIf="selectedwallet === coinwallet[1]" role="tabpanel" class="tab-pane active" id="two">wallet 2 content</div>
</div>

.ts:

  coinwallet: string[] = ['wallet1','wallet2'];
  selectedwallet = this.coinwallet[0];

check working code here

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

4 Comments

Thanks for your reply and the link you've mentioned is working fine but when I edited that code according to my project needs, it's not working export class AppComponent { coinwalletTab: string[] = ['wallet1','wallet2']; selectedwalletTab = this.coinwalletTab[0]; coinwallet: string[]; selectedwallet: any; OnInit(){ this.coinwallet = ['wallet1', 'wallet2']; this.selectedwallet = this.coinwallet[0]; }
leave OnInit() blank and why did you declare 'selectedwallet: any'?
@NitishKumar show me your code somewhere like in stackblitz or jsfiddle or just update your question with your complete code.
how to create tab-content also dynamically?
0

I think rather than converting HTML tabs to Angular, You can directly user ng-bootstrap components which works very well with Angular and comes with lot of configurable options. below is a link for ng-bootstrap tabs

https://ng-bootstrap.github.io/#/components/tabs/examples

1 Comment

Thanks for your reply, But I have already created those tabs and if I change the tabs, I would have to change a lot of content so that won't be a solution

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.