angular - How to assign to variable true in pipe?
问题描述
If id is null I want variable x to be true. I would use if and else but can't do that in pipe. Please help me.
private x = false;
private y = false;
ngOnInit() {
this.subscribe = this.route.params.pipe(
map(({ id }) => id),
filter(id => !!id), // <---- here
switchMap((id: string) =>
this.shippingService.getShippingById(id)))
.subscribe(
res => {
this.shippingData = res;
this.y= true;
},
err => this.error = err.error,
);
}
解决方案
您可以tap
在过滤器之前使用运算符:
ngOnInit() {
this.subscribe = this.route.params.pipe(
map(({ id }) => id),
tap((val) => { if (val === null) this.x = true }),
filter(id => !!id),
switchMap((id: string) =>
this.shippingService.getShippingById(id)))
.subscribe(
res => {
this.shippingData = res;
this.y= true;
},
err => this.error = err.error,
);
}
推荐阅读
- python - Python 中的异常中断
- jenkins - 无法在 Jenkins 从节点上运行赛普拉斯测试?
- javascript - 在 Javascript 中使用 Maps 实现二维数组
- sql - 过滤相同的查询 3 次不同的时间。表现?
- python - signal.medfilt2d 得到 ValueError: object too deep for desired array
- machine-learning - 如何在 Imblearn 中获取已创建样本的索引
- reactjs - 如何使用 SVG 代码作为元素的来源?
- java - JPA 查询:java.lang.IllegalArgumentException:提供了至少 1 个参数,但查询中仅存在 0 个参数
- node.js - 操作符集在 mongodb 中不起作用。或任何其他方法将新字段添加到文档中?
- java - 如何使用 RandomAccessFile 恢复中断下载?