Issue
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
});
}
Solution
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])
});
}
Answered By - ambussh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.