我试图在我的组件中填充一个数组,processes该数组是一个数组process.每个process还有一个列表tasks.
所以目前,我正在使用两个api调用:
/processes
和
/process/{processId}/tasks
我/processes用来获取所有进程并最初填充processes数组.然后我使用每个进程id process调用第二个API来获取该进程的任务.
目前,我的代码看起来像这样:
this.processes.forEach((process, index) => {
myService.getTasks().subscribe((tasks) => {
process.tasks = tasks;
})
})
Run Code Online (Sandbox Code Playgroud)
我知道我可以创建一个observable数组,并Observable.forkJoin()用来等待所有这些异步调用完成,但我希望能够为每个调用定义subscribe回调函数,因为我需要引用它process.关于如何处理这个问题的任何想法?
我有一个 api 函数,它执行 post 并返回 ModelAResponse 的 Observable (我拥有的接口)
我希望两个根据返回的状态返回 ModelAResponse 或 B 的 Observable。
这是我的两个响应模型:
export interface ModelAResponse {
res: ModelA;
}
export interface ModelBResponse {
res: ModelB
}
Run Code Online (Sandbox Code Playgroud)
(ModelA、ModelB是放置在另一个类中的接口)
所以目前我只支持模型 a 的一次返回:
public myApiFunc(req: MyRequestModel): Observable<ModelAResponse> {
...
this.http.post("my/api/path", req, {headers: myHeaders}),
(jsonReturned) => status === 200 ? {res: jsonReturned} : undefined);
...
}
Run Code Online (Sandbox Code Playgroud)
如何更改此函数以根据打字稿最佳实践方式的状态返回 ModelAResponse 或 ModelBResponse?
我想调用两个 observable,在第一个之后调用第二个。
我有一个 observable 也是先订阅的。然后我希望第二个 observable 嵌套在第一个 observable 中。
我想在下面结合这两个订阅:
this.serviceA.getData()
.takeUntil(destroyed$)
.subscribe(item => {
});
this.serviceB.getData()
.takeUntil(destroyed$)
.subscribe(item => {
});
Run Code Online (Sandbox Code Playgroud)
这是我尝试展示我想做的事情(更新)
destroyed$: Subject<any> = new Subject<any>();
ngOnInit(): void {
this.serviceA.getData
.takeUntil(this.destroyed$)
.concatMap(dataA => {
this.myDataA= dataA;
return this.serviceB.getData;
})
.takeUntil(this.destroyed$)
.subscribe(dataB => {
this.myDataA.forEach(item => {
item.visible = dataB.find(o => o.id === item.label).isVisible;
});
});
};
ngOnDestroy(): void {
this.destroyed$.next(true);
this.destroyed$.complete();
};
Run Code Online (Sandbox Code Playgroud)
目的是在第一个订阅之后调用第二个嵌套订阅,因为第二个订阅中需要第一个订阅者的数据
我有一项服务从服务器获取一些数据并更新一些主题,在我的组件的 ngOnInit 函数中我正在订阅该主题。我已确认主题已正确更新。
以下是我的服务功能,它从服务器获取数据并更新主题。
getInboxData(id?) {
this.inboxID = id;
this.loadingData.next( // tells to start showing loading animation
{
showloading: true,
clearDocuments: this.shouldClearDocuments()
});
this.getInboxCount();
this.dashboardService.getInbox(id, this.page)
.subscribe((response) => {
for (const inbox of response['results']) {
this.documents.push(inbox.document);
// tells to stop showing loading animation
this.loadingData.next({showloading: false, clearDocuments: this.shouldClearDocuments()});
}
if (response.next != null) {
this.page++;
// this.getInboxData(id);
}else {
this.page = 1; // reset
}
this.documentsData.next(response);
});
}
Run Code Online (Sandbox Code Playgroud)
我正在我的组件中订阅documentsData。以下是组件的代码。
ngOnInit() {
this.dashboardDataService.getInboxData();
this.dashboardDataService.documentsData
.subscribe((response) => {
this.isLoadingMoreResult = false; …Run Code Online (Sandbox Code Playgroud) 我有这样的订阅:
this.test.subscribe(params => {
...some code
});Run Code Online (Sandbox Code Playgroud)
如果我通过回调函数而不是箭头函数,则上下文丢失。
我想将上下文绑定到订阅函数,但我从未见过。是否可以不做类似的事情
that = thisRun Code Online (Sandbox Code Playgroud)
试图做一个简单的计时器,需要在某些if条件下打破它.但总是出错EXCEPTION: timer.unsubscribe is not a function.
我究竟做错了什么?
let timer:any = Observable.timer(0,1000);
timer.subscribe(data => {
console.log(this.itemsRatedList);
if(data == 5) timer.unsubscribe();
});
Run Code Online (Sandbox Code Playgroud) 所以我有两个observable,一个返回当前类别,其他产品.我希望根据类别过滤产品.
这是在Angular 2中,所以我真的希望我的ng2-view成为订阅者(通过异步管道).
像这个简单的例子:
let category$ = Observable.of({id: 1});
let products$ = Observable.from([{name: 'will be included', cat_ids: [1, 5]}, {name: 'nope', cat_ids: [2, 3]}, {name: 'also yep', cat_ids: [1, 7]}]);
return products$
.toArray()
.filter(prod => {
return prod.cat_id.some(id => id === <how do I get the value of the category observable here?>)
});
Run Code Online (Sandbox Code Playgroud)
也许答案很简单,但它让我望而却步.
我有一个通过一个可观察到的对象得到的对象compModel.它给了我一个大的配置对象,我需要用它来调整dom片段.我想要做的一件事是根据这个模型选中/取消选中一些复选框,但我无法访问它们各自的属性.
<input type="checkbox" checked="{{compModel | async}}">
null并且不会在任何时候检查该框.我认为这被解释为将obj一个observable 的属性传递给异步管道,它返回null因为observable本身没有obj,所以未来的数据会这样做.<input type="checkbox" checked="{{compModel.obj | async}}">
Cannot read property boolean of undefined.这里的布尔值就是我所追求的.我怎么得到它?<input type="checkbox" checked="{{compModel.obj.boolean | async}}">
所以这是场景.我有一个带有BehaviorSubject的用户服务和一个返回此BehaviorSubject的observable的方法.我的第二个文件是订阅observable的标题组件.问题是..是否可以只订阅更改或者我需要在之前有一些逻辑this.userSubject.next(this.user)吗?
这里是参考代码:
// user.service.ts
user: User;
private userSubject = new BehaviorSubject<User>(new User({}));
keepUpdated = () => {
this.tokenService.tokenStream()
.subscribe(token => {
this.user.update(token);
this.userSubject.next(this.user);
});
}
Run Code Online (Sandbox Code Playgroud)
和这里
// header.component.ts
ngOnInit() {
this.userService.keepUpdated();
this.userService.userStream()
.subscribe(user => {
// Here is the problem. This console.log gets called everytime userSubject.next(this.user) send something. I would like it only only to be called if the user is different from the previous one.
console.log(user);
});
}
Run Code Online (Sandbox Code Playgroud) 我有一系列通过循环遍历小工具的Http调用.有没有办法中止所有请求
for (let gadget of gadgets) {
this.userService.getGadgetsData(gadget.Id, gadget.Name).subscribe(gadgetsData => {
});
}
Run Code Online (Sandbox Code Playgroud)
我在component.service.ts中的服务代码
@Injectable()
export class UserService {
constructor(public _http: Http) { }
getGadgetsData() {
return this._http.get(this._dashboards, { headers: this.getHeaders() })
.map((res: Response) => res.json());
}
}
Run Code Online (Sandbox Code Playgroud)