Angular2 Toggling showing and hiding between two elements I have a tabs nav element that has two tabs, it needs to show a component based on what tab was clicked and hide the other component.  If the clicked tab is already "active", the component needs to remain showing.

I have this working, but it seems very inefficient to me.  Can anyone show me a better way to do this?

Here's how I have it set up now.  For the sake of not posting every file in the question, know that the project is set up correctly.

@Component({
  selector: 'my-app',
  template: `
    &lt;div&gt;
      &lt;button type="button" (click)="changeShowStatus(oneShowing=true,twoShowing=false)"&gt;1&lt;/button&gt;
      &lt;button type="button" (click)="changeShowStatus(twoShowing=true,oneShowing=false)"&gt;2&lt;/button&gt;
      &lt;div class="box1" *ngIf="oneShowing"&gt;
        &lt;p&gt;some content&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="box2" *ngIf="twoShowing"&gt;
        &lt;p&gt;some content2&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  `,
})
export class App {
  name:string;
  oneShowing:boolean;
  twoShowing:boolean

  constructor() {
    this.oneShowing = true;
    this.twoShowing = false
  }
}


Plunker
Separate content and logic

It is a good practice (recommended by Angular style guidelines) to separate component template and component code.

Move template to an own my-app.component.html file and refer to it via templateUrl property instead of template.

Avoid logic in your template

oneShowing=true,twoShowing=false in and twoShowing=true,oneShowing=false parts are technically logic.
It does need to be in the template, and it should not be there.

my-app.component.html

&lt;button type="button" (click)="displayBoxWithNumber(1)"&gt;1&lt;/button&gt;
&lt;button type="button" (click)="displayBoxWithNumber(2)"&gt;2&lt;/button&gt;


my-app.component.ts

displayBoxWithNumber(boxToShow: number): void {
  this.hideAllBoxes();
  this.displayBox(boxToShow);
}
private hideAllBoxes(): void {
  this.oneShowing = false;
  this.twoShowing = false;
}
private displayBox(boxToShow: number): void {
  if (boxToShow === 1)
    this.oneShowing = true;
  else if (boxToShow === 2)
    this.twoShowing = true;
  else
    throw new Error(`Invalid box number provided ${boxToShow}`);
}


Please notice that the code above is for illustrational purposes in your specific context.
Any more or less real project will use *ngFor directive to iterate over some data source to construct the boxes dynamically.
Something like the code below.
Do not worry if you don't understand it yet.
You'll get there later with a bit of practice.

&lt;div *ngFor="let box of boxeList; boxIndex = index; trackBy: trackByBoxId"&gt;
  &lt;button type="button" (click)="displayBox(box, boxIndex)"&gt; {{ box.number }} &lt;/button&gt;
  &lt;div class="box" *ngIf="isBoxActive(box)"&gt;
    &lt;simple-box-view [value]="box"&gt;&lt;/simple-box-view&gt;
  &lt;/div&gt;
&lt;/div&gt;


Know your tool/language

In typeScript you can initialize class' properties inline (this is the same as setting the values in constructor).
You can also omit the type of the variable it is inferred.
Similarly, if your constructor does not do any work, it can be got rid of.

export class App {
  name: string;
  oneShowing = true;
  twoShowing = false;

  // Methods...
}