角反应形式值随管道变化.这是一个错误??? [英] angular reactive form valuechanges with pipe. IS THIS A BUG ???

查看:28
本文介绍了角反应形式值随管道变化.这是一个错误???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎管道不适用于反应式表单控件 valueChanges.我准备了这个 https://stackblitz.com/edit/angular-vdeqrz 以便你可以重现这个问题.

Seems like pipe doesn't work with reactive form control valueChanges. i've prepared this https://stackblitz.com/edit/angular-vdeqrz so that you can reproduce the problem.

在文本字段中输入一些内容.然后输入boom"(不带引号).错误捕获后,控件不再起作用.之后您可以验证在其上输入新内容.它不会检测到任何其他输入.

type something in the text field. then type "boom" (without the quotes). after the error catch, the control does not work any more. you can verify typing something new on it after that. it does not detect any other input.

如果您刷新页面,则它会再次工作.

if you refresh the page, then it works again.

我在这里遗漏了什么吗?

am i missing something here ?

推荐答案

这不是错误.每个 Rx 流可以发出零个或多个 next 通知和一个 errorcomplete 通知,但不能同时发出.

This is not a bug. Every Rx stream can emit zero or more next notifications and one error or complete notification but never both.

因此,如果您使用 throwError,该链将自行处理,因为您永远不会让同一条链发出两个或多个 error 通知,而这正是您所期望的.

So if you use throwError the chain will dispose itself because you can never have the same chain emitting two or more error notification which is what you expect.

显然有很多方法可以避免这种情况.例如,您可以使用与 throwError 不同的东西,并用某个对象包装输入值.但是你将无法在观察者的 error 处理程序中处理它.

There're obviously many ways to avoid this. You can use something different than throwError and wrap the input value with some object for example. But then you won't be able to handle it in observer's error handler.

你也可以使用retrytap来处理错误:

You can also use retry and tap to handle the error:

this.query.valueChanges
  .pipe(
    switchMap(d => (d === 'boom') ? throwError(new Error("boom")) :  of(d)),
    tap({ error: err => console.log(err) }),
    retry(),
  )
  .subscribe({
    next: d => console.log(d),
    complete: () => console.log("complete")
  })

你必须在这里使用 tap 因为错误永远不会到达观察者.

You'd have to use tap here because the error never reaches the observers.

您更新的演示:https://stackblitz.com/edit/angular-1qfn2z?file=src/app/app.component.ts

这篇关于角反应形式值随管道变化.这是一个错误???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆