【发布时间】:2017-02-14 10:55:33
【问题描述】:
我有一个案例,我在 angular2 组件中使用异步管道并将 observable 传递给它。效果很好。
我想要的是稍后,当组件仍然“活动”时,即:尚未调用 OnDestroy(因此订阅了异步管道),取消订阅该 observable 并订阅另一个。
我怎样才能做到这一点?
这里有一些伪代码来揭露这个案例:
@Component({
selector: 'pack',
template: `
<wolf *ngFor="let wolf of pack | async">
...
</wolf>
`
})
export class PackComponent implements DoCheck {
pack;
constructor(){
this.pack = Observable.of(northwesthernWolves: Wolf[])
}
onDoCheck() {
if(some condition) {
this.pack = Observable.of(redWolves: Wolf[])
}
}
}
【问题讨论】: