I'm using angular 2 rc1 and the new router @angular/router
In app component I have a nav section and router outlet
<nav>
<a *ngIf="auth?.userName == null" [routerLink]="['/login']">Login</a>
<a *ngIf="auth?.userName != null" (click)="logout()">Logout</a>
{{auth?.userName}}
</nav>
<router-outlet></router-outlet>
I inject a loginService into app component and subscribe to an event in ngOnInit
ngOnInit() {
this.loginService.loginSuccess.subscribe(this.loginSuccess);
}
ngOnDestroy() {
this.loginService.loginSuccess.unsubscribe();
}
private loginSuccess(res: IAuthResponse) {
this.auth = res;
}
when I navigate to my '/login' route page/component my loginService is inject and loginService defines an event
@Output() loginSuccess = new EventEmitter<IAuthResponse>();
and after successful login in my service, the login service raises the event
this.loginSuccess.next(response);
I'm able to set a breakpoint in app component on the subscribed loginSucess and the data is passed along, however 'this' is undefined.
private loginSuccess(res: IAuthResponse) {
this.auth = res; //this is undefind
}
how can I update the app component from events that are triggerd from services used in components that are hosted in the router outlet