Md Ghouse Md Ghouse - 1 month ago 18
TypeScript Question

Angular2 How to call a function inside subscribe

Please Help me How can i call a function inside a subscribe.In Below code if User successfully login he redirect to some other page based on the condition

Login(UserName: string, Password: string) {
var token = this.LoginSer.access_token;
if (UserName != null && Password != null) {
return this.LoginSer.LoginDetails(UserName, Password)
.subscribe(data => {
this.isAuthenticated = data, alert('successfullyyy...'), this.LogedIn
}, error => alert('Given Details Not Matching...'))
}


I Implement some method Here for conditional based Routing And i call This Method inside the subscribe as This.LogedIn

LogedIn():void {
if (localStorage.getItem('currentUser')) {
var cur = JSON.parse(localStorage.getItem('currentUser'));
var user = cur.username;
if (user == "admin") {
this.router.navigate[("Admin/Add")]
}

}

Answer Source

I think you need this, Function should be called as this.LogedIn();

 if (UserName != null && Password != null) {
            return this.LoginSer.LoginDetails(UserName, Password)
                .subscribe(data => {
                    this.isAuthenticated = data;
                    alert('successfullyyy...');
                    this.LogedIn();
                }, error => alert('Given Details Not Matching...'))
 }