Typescript 我如何从实用的角度连接观察对象?
我有一个组件,如下所示Typescript 我如何从实用的角度连接观察对象?,typescript,angular,observable,fork-join,Typescript,Angular,Observable,Fork Join,我有一个组件,如下所示 @Component({ 'selector': 'my-app', 'template': ` <!--processed data from the constructor--> <ul> <li *ngFor="let stuff of item"> {{stuff}}</li> </ul> <button
@Component({
'selector': 'my-app',
'template': `
<!--processed data from the constructor-->
<ul>
<li *ngFor="let stuff of item"> {{stuff}}</li>
</ul>
<button (click)="getMoreDetail()">Get More Detail</button>
`
})
export class AppComponent{
public item;
constructor(private _service:SampleService){
this.getDetail();
}
public getDetail(){
Observable.forkJoin(
this._service.getStuff1()
).subscribe(
res => {
this.item = res[0];
},
null,
() => {
}
);
}
public getMoreDetail(){
// i want to join this._service.getStuff2() to the
// Observable.forkJoin on this.getDetail with this method.
}
}
@组件({
“选择器”:“我的应用程序”,
“模板”:`
- {{stuff}
获取更多细节
`
})
导出类AppComponent{
公共项目;
构造函数(私有服务:SampleService){
这是getDetail();
}
公共获取详细信息(){
可观察的分叉连接(
这是。_service.getStuff1()
).订阅(
res=>{
this.item=res[0];
},
无效的
() => {
}
);
}
公共getMoreDetail(){
//我想加入此。_service.getStuff2()到
//使用此方法在this.getDetail上创建Observable.forkJoin。
}
}
基本上,当加载组件时,一些数据是使用getDetail
方法加载的,该方法首先在页面上呈现。当用户单击“获取更多详细信息”按钮时,我想将服务调用(this.\u service.getStuff2()
)加入到可观察对象
我怎样才能做到这一点
如果不可能,我如何用更少的资源处理这种情况
该组件是一个虚拟组件,省略了提供程序
谢谢。所以您需要延迟加载后逻辑,直到所有数据加载完毕?当您想要执行多个并发HTTP请求时,您可以使用
forkJoin
,我认为这不是您的情况。.我只需要在按钮单击时调用getmoredeail()
后加载第二个服务。为什么不在getDetail
完成后才显示按钮,因此,在该方法中添加代码以显示您的更多详细信息按钮
@AngJobs好吧,谢谢您的建议,我只关心在这一点上连接可观察对象。所以您需要延迟加载后逻辑,直到加载完所有数据?当您想要执行多个并发HTTP请求时,您可以使用forkJoin
,我认为这不是您的情况。.我只需要在按钮单击时调用getmoredeail()
后加载第二个服务。为什么不在getDetail
完成后才显示按钮,因此,在该方法中添加代码以显示您的更多详细信息按钮
@AngJobs好吧,谢谢您的建议,我只关心在这一点上连接可观察对象。