Reputation: 103
There is an authorization form:
<form (submit)="login($event)">
<input class="form-control" name="PromoCode" placeholder="enter promo">
<input type="submit" class="btn btn-block btn-outline-primary" value="login">
</form>
And there is a method login
:
isExist : boolean = false;
...
login(e){
var promo = e.target.elements[0].value;
this.http.get('http://localhost:49296/api/Buyers/GetBuyer?promo=' + promo).subscribe((exist : boolean) => this.isExist = exist);
console.log(this.isExist);
if(this.isExist == true){
this.router.navigate(['books']);
}
}
localhost
is my Web API project. I check requests in Fiddler and Web API give me correct value. But when I click on login, isExist
change only before second click. How can I fix this?
Upvotes: 0
Views: 33
Reputation: 6983
Move the navigation logic to the subscribe method. The http.get
returns a observable. That means it is asynchronus. So you have to move all the to the subcribe method.
login(e){
var promo = e.target.elements[0].value;
this.http.get('http://localhost:49296/api/Buyers/GetBuyer?promo=' + promo).subscribe((exist : boolean) => {
this.isExist = exist
console.log(this.isExist);
if(this.isExist == true){
this.router.navigate(['books']);
}
});
}
Upvotes: 1