I do have an Observable of Contacts in my onInit method and I also will set the first contact of the Observable into my form. Has anyone a hint how to do this?
ngOnInit() {
let contactsObservable: Observable<Contact[]> =
this.contactService.getAllContacts();
contactsObservable.subscribe(contacts => {
this.contacts = contacts;
});
this.form = this.fb.group({
// this.contacts[0] does not work
contact: [this.contacts[0], [Validators.required]] // how to get contact[0] into form for presetting
});
}
You get contacts async. Value can be put when Observable return value.
ngOnInit() {
this.form = this.fb.group({
contact: ['', [Validators.required]]
});
let contactsObservable: Observable<Contact[]> =
this.contactService.getAllContacts();
contactsObservable.subscribe(contacts => {
this.contacts = contacts;
this.form.controls['contact'].setValue(this.contacts[0])
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With