I have a service :
@Injectable()
export class LostFoundEditService {
public lostForm: FormGroup;
public countries: any[] = [];
private countriesUrl = 'assets/countries.json';
constructor(private http: HttpClient) { }
init() {
this.initForm();
this.http.get(this.countriesUrl).subscribe(data => {
this.countries = this.countries.concat(data['countries']);
},
(err: HttpErrorResponse) => {
console.log(err.message);
});
}
private initForm() {
this.lostForm = new FormGroup({
'title': new FormControl('', Validators.required),
'description': new FormControl('', Validators.required),
'country': new FormControl('', Validators.required),
'state': new FormControl('', Validators.required),
'city': new FormControl('', Validators.required),
'zipCode': new FormControl(''),
'street': new FormControl('')
});
}
}
and a class thats uses this service :
@Component({
selector: 'app-lost-edit',
templateUrl: './lost-edit.component.html',
styleUrls: ['./lost-edit.component.css']
})
export class LostEditComponent implements OnInit {
lostForm: FormGroup;
countries: any[] = [];
states: any[] = [];
cities: any[] = [];
constructor(
private http: HttpClient,
private lostFoundEditService: LostFoundEditService) { }
ngOnInit() {
this.lostFoundEditService.init();
this.lostForm = this.lostFoundEditService.lostForm;
this.countries = this.lostFoundEditService.countries;
}
onCancel() {
}
}
Also I have the template associated with that class :
(...)
<select
id="country"
formControlName="country"
class="form-control">
<option value="">Countries</option>
<option *ngFor="let country of countries" value="{{country['id']}}">{{country['name']}}</option>
</select>
</div>
</div>
</div>
(...)
My question is : how to wait for the end of subscribe method (in init() of LostFoundEditService) to have all countries of the json file entered in countries array of LostEditComponent. Nothing appears in the dropdown list for the moment...