1

On clicking on one tab i want to make the class tab-active and remove it from the other and vice versa. The HTML code on which I want to implement this is :-

<div class="tab-change-login">
    <ul class="un-styled tab-ul">
        <li class="tab-active" data-login="signin-area">SIGN IN</li>
        <li data-login="signup-area">SIGN UP</li>
    </ul>
</div>

How do I write a onClick function to switch between 2 tabs usiing angular2

1 Answer 1

3

You can use class binding like:

@Component({
  selector: '...',
  template: `
    <div class="tab-change-login">
        <ul class="un-styled tab-ul">
            <li [class.tab-active]="activeTabName == 'signin-area'" 
                data-login="signin-area"
                (click)="activeTabName = 'signin-area'">SIGN IN</li>
            <li [class.tab-active]="activeTabName == 'signup-area'" 
                data-login="signup-area"
                (click)="activeTabName = 'signup-area'">SIGN UP</li>
        </ul>
    </div>
`})
export class MyComponennt {
  activeTabName = 'signup-area';
}

There are other ways like ngClass

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

2 Comments

Hi, thanks that worked but now if I have to hide a particular portion depending on which tab is selected will using ngIf to check the activeTabName value and hide/unhide accordingly work?

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.