Note : please ignore syntax error for this post.
My project setup is as follow.
boot.ts <= ParentComponet, has
<routerOutlet>
& sub components, contains navigation for sub coponents.
-----Home.ts <= default view usinguseAsDefault
-----login.ts <= but by overridingrouterOutlet
, I check whether user is authenticated or not. If not (here obviously), load login component.
models.ts
export class UserModel()
{
private userName:string;
private isLoggedIn:boolean;
}
authService.ts
import {UserModel} '.../models.ts'
...
...
public let um=new UserModel(); // I don't know how to go ahead from here.
constructor()
{
um.userName="Guest";
um.loggedIn=true; // lets not deal with it for now.
}
setUserDetail(username:string)
{
um.userName=username;
um.loggedIn=true;
Please note here that when it changes um model, I want to update boot.html view
}
getUserDetail:UserModel()
{
return um;
}
authentication(username:string,password:string)
{
...// I have http server call here. but not shown.
... //database query
um.userName= "field from DB";
um.loggedIn=true;
...//return observable
}
login.ts ...
login(username,password)
{
auth.authentication(username,password)
.subscribe(() => {
auth.setUserDetail(username); // this should update boot.html view immediately.
});
}
boot.ts
import {UserModel} '.../models.ts';
import {authService} '.../authService.ts';
constructor(auth:authService)
{
console.log(auth.getUserDetail()); //log is correct. no problem till here.
// Note: When we run the app, it may get UserModel object and display to VIEW. But after login how can I update boot.html view?
1) how to bind upcoming object value to view?
2) when page is refreshed same object should be returned.
}
boot.html
...
<li ngIf="{{um.loggedIn}}">{{um.userName}}</li>
...
Do I need to use EventEmitter
?
http://plnkr.co/edit/uPb0eHXPke41zSa9IBdS?p=preview
This shows how to update two views using shared service.
if anyone is interested to know about overriding routerOutlet
Angular 2 Authentication with child routes