After a month of experimenting with Angular and being relatively new to web development, I've encountered an issue that I hope someone can help me with. In my simple Angular application, I have a button called roomSearch()
which sends form data to an MVC Web API. The data retrieved from the API is then displayed on the HTML page by the Angular component.
The problem is that I always have to click the button twice in order to see the result. It never works on the first click, and I'm struggling to understand why.
Here is the code snippet:
HTML Section:
<form #roomForm="ngForm" class="coupon-form" (ngSubmit)="onSubmit(roomForm)">
<div class="row">
...
</div>
<div class="form-group">
<button type="button" (click)="roomSearch(roomForm)" class="btn btn-lg btn-block btn-info"><i class="fa fa-floppy-o" aria-hidden="true"></i> Submit</button>
</div>
</form>
...
Component File:
show: boolean;
ngOnInit() {
this.show = this.roomService.show;
}
roomSearch(form: NgForm)
{
this.show = this.roomService.getRoomList(form.value.startDate, form.value.endDate);
}
Service File:
constructor(private http: Http) {
this.show=false;
}
getRoomList(startDate: string, endDate:string){
...
The issue arises when I try to log the this.roomList
on the console. It initially shows as undefined on the first click but displays the correct value on the second click. Can anyone point out what I might be doing wrong here?