I will preface that I am quite new to Angular2. I am trying to display async data on a modal, modal is rendering prior to variables updating.I would prefer to see my variables displayed.
The variables are winnername & winnerurl, Subject code is at the bottom of the .ts and .html snippets.
Thank You in advance for any input.
import { Component, ViewChild, OnInit } from '@angular/core';
import { FormsModule } from "@angular/forms";
import { findfriendService } from '../findfriend.service'
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css'],
providers: [ findfriendService ]
})
export class SearchComponent implements OnInit {
@ViewChild('myModal') modal: any;
constructor(private _findfriendService: findfriendService) { }
//Submit form
onSubmit(f){
console.log("from click: ", f);
//post form data to MongoDB
this._findfriendService.postFriend(f);
//get all current friends from Mongo DB
this._findfriendService.getFriends()
.subscribe(allFriends =>{
{ CODE REMOVED FOR EASE OF VIEWING }
// Code resulting value being assigned to variable index
for(var a=0;a<plyrsDiff.length;a++){
var b = plyrsDiff[a];
if(plyrsDiff[a]<c){
c = b;
index = b;
}
}
//CONSOLE LOG DISPLAYS VALUES CORRECTLY
winnername = allFriends[index].firstName;
winnerurl = allFriends[index].url;
this.modal.open();
})
}
<div class="container">
<div class="jumbotron">
<!--**** Begin of Form ***********************************-->
<!--Form variable #f declared -->
<form #f="ngForm" (ngSubmit)="onSubmit(f.value)">
<div class="form-group">
<!--Enter First Name-->
<label
for="firstName">First Name.
</label>
<input
ngModel name="firstName"
#firstName="ngModel"
class="form-control"
required
minlength="3">
<!-- Show Error if invalid -->
<div
*ngIf="firstName.touched && firstName.errors">
<div
class="alert alert-danger"
*ngIf="firstName.errors.required">
First name is required
</div>
</div>
<!-- Loop generates 10 user questions-->
<div
*ngFor="let question of questions; let in = index">
<h5>
{{question.q}}
</h5>
<!-- display questions, generate select options, assign variable unique to question for ngModel - name - id-->
<select
required
[(ngModel)]=numbers[in]
name={{question.id}}
id={{question.id}}>
<option
*ngFor="let choice of choices"
[ngValue]="choice">{{choice.label}}
</option>
</select>
</div>
<button
[disabled]="!f.form.valid"
type="submit"
class="btn btn-danger btn-lg">Submit
</button>
</div>
<modal #myModal>
<modal-header>
<h1>ALERT</h1>
</modal-header>
<modal-content>
<img src="{{winnerurl}}" height="160">
</modal-content>
<modal-footer>
<button class="btn btn-primary" (click)="myModal.close()">close</button>
</modal-footer>
</modal>
</form>
</div>
</div>