I am new to angular and I have the following situation which is I have a service getAnswers():Observable<AnswerBase<any>[]>
and two components that are related to each other.
- online-quote
- dynamic-form
online-quote component calls the service getAnswers():Observable<AnswerBase<any>[]>
in its ngOnInit()
method and the result of this, is passed to the component dynamic-form.
To illustrate the situation this is the code of my two components:
online-quote.component.html:
<div>
<app-dynamic-form [answers]="(answers$ | async)"></app-dynamic-form>
</div>
online-quote.component.ts:
@Component({
selector: 'app-online-quote',
templateUrl: './online-quote.component.html',
styleUrls: ['./online-quote.component.css'],
providers: [DynamicFormService]
})
export class OnlineQuoteComponent implements OnInit {
public answers$: Observable<any[]>;
constructor(private service: DynamicFormService) {
}
ngOnInit() {
this.answers$=this.service.getAnswers("CAR00PR");
}
}
dynamic-form.component.html:
<div *ngFor="let answer of answers">
<app-question *ngIf="actualPage===1" [answer]="answer"></app-question>
</div>
dynamic-form.component.ts:
@Component({
selector: 'app-dynamic-form',
templateUrl: './dynamic-form.component.html',
styleUrls: ['./dynamic-form.component.css'],
providers: [ AnswerControlService ]
})
export class DynamicFormComponent implements OnInit {
@Input() answers: AnswerBase<any>[];
constructor(private qcs: AnswerControlService, private service: DynamicFormService) { }
ngOnInit() {
this.form = this.qcs.toFormGroup(this.answers);
}
My question is what is the correct way to pass the information from online-quote to dynamic-form if the result information of the service getAnswers():Observable<AnswerBase<any>[]>
is a observable.
I've tried it in many ways but it does not work. I would like someone to help me with this. Thank you very much!