I have an angular2 application in which I have a login page. After login, I would like to display the user name in the header component.
@Output() getUserName = new EventEmitter();
In the authenticate method of service
authenticate(loginRequest) {
let headers = new Headers({ 'Content-Type' : 'application/json' });
let options = new RequestOptions({ headers: headers });
let authenticateResp = this.http.post("http://localhost:8080/authenticate", JSON.stringify(loginRequest), options).map(result => result.json());
authenticateResp.subscribe((authenticate => {
this.getUserName.emit("My Name");
}));
return authenticateResp;
}
In the header component in constructor
@Component({
.....
providers:[LoginService]
})
export class HeaderComponent {
userName : string;
constructor(private loginService : LoginService) {
loginService.getUserName.subscribe(authenticated => this.setName(authenticated));
}
setName(authenticated) {
this.userName = authenticated;
}
}
When I debug, I can see the event emitting code getting executed, but the code in subscribe in the component is not called. What am I doing wrong here?
Appreciate any help