Angular 等待订阅完成并为其他函数赋值
Angular wait until subscribe is done and give values to other function
我有以下功能
文件:subcategory.service.ts
getSubCategoriesById(inp_subCatid: String): Observable<any>{
this.getSubCategoriesList().snapshotChanges().pipe(
map(changes =>
changes.map(c =>
({ key: c.payload.key, ...c.payload.val() })
)
)
).subscribe(subCategories => {
subCategories.filter(function (subCat) {
return subCat.id == inp_subCatid;
});
});
我正在调用以下文件中的顶级函数
文件:subcategory.page.ts
this.SubCategoryService.getSubCategoriesById(subCatid).subscribe((subCategories: any) => {
this.subCat = subCategories ;
})
我遇到的问题是我收到以下错误消息:
错误类型错误:"this.SubCategoryService.getSubCategorysById(...) is undefined"
我想在从文件加载数据时获取数据"subcategory.service.ts"
希望有人能帮助我。
你的方法应该是这样的:
getSubCategories(inp_subCatid: string): Observable<any> {
return this.getSubCategoriesList().snapshotChanges().pipe(
map(changes => changes.map(c =>
({ key: c.payload.key, ...c.payload.val() })
).filter((subCat) => subCat.id === inp_subCatid)
));
}
那么你就可以这样使用了:
this.subCategoryService.getSubCategories(subCatid)
.subscribe(subCategories => this.subCat = subCategories);
如果我正确地解释了您的方法,在我看来您正在使用 firebase...如果是这样,在您第一次致电 this.yourService.getSubCategories(subCatid)
后,您的订阅将保持有效,以便您的子类别将针对数据库中的每个更改进行更新,即使您更改了 subCatid
,之前的数据库查询仍然有效。为避免这种情况,我建议您只发射一次 snapshotChanges()
:
getSubCategories(inp_subCatid: string): Observable<any> {
return this.getSubCategoriesList().snapshotChanges().pipe(
// finish the subscription after receiving the first value
take(1),
map(changes => changes.map(c =>
({ key: c.payload.key, ...c.payload.val() })
).filter((subCat) => subCat.id === inp_subCatid)
));
}
非常感谢
如果我想过滤特定数据怎么办??喜欢 "id"
getSubCategoriesbyId(inp_subCatid): Observable<any>{
this.getSubCategoriesList().snapshotChanges().pipe(
map(changes =>
changes.map(c =>
({ key: c.payload.key, ...c.payload.val() })
)
)
).subscribe(subCategories => {
subCategories.filter(function (subCat) {
return subCat.id == inp_subCatid;
});
});
}
然后取回过滤后的数据
this.yourService.getSubCategoriesbyId(subCatid)
.subscribe(subCategories => console.log(subCategories));
我有以下功能
文件:subcategory.service.ts
getSubCategoriesById(inp_subCatid: String): Observable<any>{
this.getSubCategoriesList().snapshotChanges().pipe(
map(changes =>
changes.map(c =>
({ key: c.payload.key, ...c.payload.val() })
)
)
).subscribe(subCategories => {
subCategories.filter(function (subCat) {
return subCat.id == inp_subCatid;
});
});
我正在调用以下文件中的顶级函数
文件:subcategory.page.ts
this.SubCategoryService.getSubCategoriesById(subCatid).subscribe((subCategories: any) => {
this.subCat = subCategories ;
})
我遇到的问题是我收到以下错误消息: 错误类型错误:"this.SubCategoryService.getSubCategorysById(...) is undefined"
我想在从文件加载数据时获取数据"subcategory.service.ts" 希望有人能帮助我。
你的方法应该是这样的:
getSubCategories(inp_subCatid: string): Observable<any> {
return this.getSubCategoriesList().snapshotChanges().pipe(
map(changes => changes.map(c =>
({ key: c.payload.key, ...c.payload.val() })
).filter((subCat) => subCat.id === inp_subCatid)
));
}
那么你就可以这样使用了:
this.subCategoryService.getSubCategories(subCatid)
.subscribe(subCategories => this.subCat = subCategories);
如果我正确地解释了您的方法,在我看来您正在使用 firebase...如果是这样,在您第一次致电 this.yourService.getSubCategories(subCatid)
后,您的订阅将保持有效,以便您的子类别将针对数据库中的每个更改进行更新,即使您更改了 subCatid
,之前的数据库查询仍然有效。为避免这种情况,我建议您只发射一次 snapshotChanges()
:
getSubCategories(inp_subCatid: string): Observable<any> {
return this.getSubCategoriesList().snapshotChanges().pipe(
// finish the subscription after receiving the first value
take(1),
map(changes => changes.map(c =>
({ key: c.payload.key, ...c.payload.val() })
).filter((subCat) => subCat.id === inp_subCatid)
));
}
非常感谢
如果我想过滤特定数据怎么办??喜欢 "id"
getSubCategoriesbyId(inp_subCatid): Observable<any>{
this.getSubCategoriesList().snapshotChanges().pipe(
map(changes =>
changes.map(c =>
({ key: c.payload.key, ...c.payload.val() })
)
)
).subscribe(subCategories => {
subCategories.filter(function (subCat) {
return subCat.id == inp_subCatid;
});
});
}
然后取回过滤后的数据
this.yourService.getSubCategoriesbyId(subCatid)
.subscribe(subCategories => console.log(subCategories));