【发布时间】:2017-11-24 21:42:44
【问题描述】:
我不能让我的函数 getData 多次工作,第一次使用字段名称“a”调用 getData,数据从 returnData 返回,并且 getData 的 .subscribe 内的块被命中。第二次使用字段“b”调用 getData,returnData 内部的所有内容都按预期工作,但是 getData 的 .subscribe 内部的块根本没有命中。
private getData(field: string): void {
this.returnData(field)
.subscribe((data) => {
//Handle Data
//This block is only hit the first time getData is called
});}
private returnData(field: string): Observable < SomeObj > {
let subj: Subject < SomeObj > = new Subject < SomeObj > ();
const obj: SomeObj = new SomeObj(field);
this.someDataServive.someFunction(field)
.subscribe(
(data) => {
//set properties on obj from data and emit obj
subj.next(obj);
//This block is always hit every time getData is called, and the
// obj returned from here is correct
},
(err) => {
subj.next(obj);
});
return (subj.asObservable());
}
【问题讨论】:
-
在标签中添加 typescript 以获得语法高亮,所有 cmets 已经很难阅读了
-
为了更好的可读性,我在这里做了一个代码:plnkr.co/edit/pylvkE6QBnqjd81nsWlS?p=catalogue
标签: typescript rxjs angular2-observables