0

I am new at Angular so in order to learn it I always create a small project. In this case I have 3 components of which only these two are important for now:

  1. TANavBarComponent
  2. TAPageContainerComponent

In my TANavBarComponent I want to 'Output' the selected Tab and use that value as 'Input' in TAPageContainerComponent. Based on the input received I will 'fill' the container with the 3rd component.

My code for TANavBarComponent:

export class TANavBarComponent {
  @Output() pageToDisplay = new EventEmitter();

  tabClick(pageName: string){
    switch(pageName){
      case '1':
        alert(pageName);
        this.pageToDisplay.emit(pageName);
        break;
      case '2':
        alert(pageName);
        this.pageToDisplay.emit(pageName);
        break;
    }
  }
}

My code for TAPageContainerComponent:

export class TAPageContainerComponent {
  title: string = "TAPageContainerComponent";
  @Input() pageToDisplay!: string;    
}

<div [ngSwitch]={pageToDisplay} class="ta-page-container" >
    <div *ngSwitchCase="'1'"><clone-user-settings></clone-user-settings></div>
    <div *ngSwitchCase="'2'"><transfer-deal></transfer-deal></div>
</div>

How do I get the 'Output' as 'Input' in order to perform the NgSwich?

1 Answer 1

2

When you emit the Output of TANavBarComponent you can set it on an attribute

<app-tabNavBar (pageToDisplay)="onPass($event)"></app-tabNavBar>

{
   elementToPass: string = '';

   onPass(pageName: string){
   this.elementToPass = pageName
}

And that same property binding to a TAPageContainerComponent input

<app-taPageContainer [pageToDisplay]="elementToPass"></app-taPageContainer>
Sign up to request clarification or add additional context in comments.

2 Comments

This helped me in a direction - thanks. Now I am stuck with a scenario where once I select the tab, the page which should be displayed in the TAPageContainerComponent is displayed for a split second and then it disappears. Any idea what could cause this?
you are likely not thinking correctly in terms of how angular works. my suggestion would be to get everything to work how you want it to work using only your container component. and then once that is working. try creating the second component and move some of your code there.

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.