【发布时间】:2018-09-14 16:25:47
【问题描述】:
我是 Angular 新手,我有以下情况,即我有一个服务 getAnswers():Observable<AnswerBase<any>[]> 和两个相互关联的组件。
- 在线报价
- 动态形式
online-quote 组件在其ngOnInit() 方法中调用服务getAnswers():Observable<AnswerBase<any>[]>,并将其结果传递给组件dynamic-form。
为了说明情况,这是我的两个组件的代码:
在线报价.component.html:
<div>
<app-dynamic-form [answers]="(answers$ | async)"></app-dynamic-form>
</div>
在线报价.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");
}
}
动态表单.component.html:
<div *ngFor="let answer of answers">
<app-question *ngIf="actualPage===1" [answer]="answer"></app-question>
</div>
动态表单.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);
}
我的问题是,如果服务getAnswers():Observable<AnswerBase<any>[]> 的结果信息是可观察的,那么将信息从 online-quote 传递到 dynamic-form 的正确方法是什么.
我已经尝试了很多方法,但它不起作用。我希望有人能帮我解决这个问题。非常感谢!
【问题讨论】:
标签: angular typescript rxjs angular2-observables angular2-decorators