【发布时间】:2019-12-29 05:46:55
【问题描述】:
有没有办法在 Angular 2 中让异步回调提供 @Input 的组件? (我现在专门使用 RC7。)
我的目标是拥有一个包装组件层次结构,作为层次结构中该级别服务的接口。这个想法是可以使用父组件的接口为子组件提供模型,等等。换句话说,父组件将提供一个getChild(ID),然后将其映射到子组件上的输入以设置其模型,所有这些都在模板中:
<parent-comp #parent>
<child-comp
*ngFor="let id of parent.childIds;"
[model]="parent.getChild(id)">
<!-- And so on, with the child's children, etc. -->
</child-comp>
</parent-comp>
父组件的方法包装了一个返回承诺的服务方法(此时)。所以父组件的getChild() 方法的内容是问题,因为我需要稍后返回实例。
我考虑过的一个解决方案是修改这些方法以传递模型实例以进行填充。
// In the parent component
public getChild(id: string): Child {
let child = new Child();
this.service.getChild(id, child);
return child;
}
// In the parent service
public getChild(id: string, model: Child): Promise<Child> {
this.http.get(some_url).toPromise().then(res => model.update(res));
}
有没有更好的方法来处理这个构造,以便父组件方法可以为子组件提供模型等等?
【问题讨论】:
标签: angular typescript