0

I am new to angular 2 and creating an application

app.routing.ts

const appRoutes: Routes = [
    {path:'', component:HomeComponent},
    { path: 'login', component: LoginComponent },
        { path: 'register', component: RegisterComponent },

    // otherwise redirect to home
    { path: '**', redirectTo: '' }
];

app.component.html

<app-navbar (sideMenuToggleClicked)="handleSideMenuToggle()"></app-navbar>
<div class="container-fluid">
    <div class="row">
        <div class="col col-sm-2 test-border" *ngIf="sideBarToggle">
            <h1>
                {{sideBarToggle }}
            </h1>
        </div>
        <div class="col col-10 test-border" [ngClass]="sideBarToggle?'col-10':'col-12'">
            <app-alert *ngIf="showAlert"></app-alert>
            <router-outlet (showAlert)="handleShowAlert($event)"></router-outlet>
        </div>
    </div>
</div>

register.component.ts

@Output() showAlert = new EventEmitter();
onSubmit(){
 this.registerService.getValues(this.registrationForm.value).then((result:any)=>{
        console.log(result)
       }).catch((error:any)=>{
         this.showAlert.emit({type:'error', message:error});
       });

      }

As you can see i was successful in capturing the click in app-navbar which is loaded as component.

Now i am trying to create a common alert notification and when i try it with

<router-outlet (showAlert)="handleShowAlert($event)"></router-outlet>

I was not able to capture the EventEmitter Click.

Question is how can transfer the emit from register.component.html to app.component.html

Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83

1 Answers1

0

It will not work like you are trying, it will try to look showAlert on RouterOutlet directive which it will not find.

you need to check how Parent and children communicate via a service.

Hope this helps!!

Madhu Ranjan
  • 17,334
  • 7
  • 60
  • 69