如何在订阅之前获得形式控制的ValueChanges值?

发布于 2025-01-30 17:19:51 字数 545 浏览 2 评论 0原文

嗨,我有父母表格控制和一个子组件。在父级控件发生更改之前,不显示子组件,例如:

<div *ngIf="parentControlChanged">
  <ChildComponent [value]="parentChanged$ | async"></ChildComponent>
</div>

如您所见,子组件使用async管道从父控件获得值是一个布尔状态,表明首先更改了父控件,并且parentChanged $只是parentcontrol.valuechanges的管道,与此问题无关的其他副作用)。

问题是在父控件发生更改之前,不显示子组件,因此尚未订阅valueechanges,因此无法获得父级控件的更改值。否则,一旦显示父母,子组件将获得父母控件的每个后续更改值。

因此,一旦父母的组件订阅了valuechanges async 管道,就可以使孩子组件获得最新的/当前值。 >行为主题?

Hi I have a parent form control and a child component. The child component is not displayed until the parent form control has changed, e.g.:

<div *ngIf="parentControlChanged">
  <ChildComponent [value]="parentChanged$ | async"></ChildComponent>
</div>

As you can see, the child component is using an async pipe to get the value from the parent control (here parentControlChanged is a boolean state to indicate the parent control is first changed and parentChanged$ is just a pipe of the parentControl.valueChanges with some additional side-effects unrelated to this question).

The issue is the child component is not displayed until the parent control has changed, and hence not subscribed to the valueChanges yet, and hence not getting the changed value of the parent control. Otherwise, the child component is getting every subsequent change value of the parent control once it is displayed.

So is there a way for the child component to get the latest/current value of the parent control once it subscribes to the valueChanges with async pipe just like subscribing to a BehaviorSubject?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2025-02-06 17:19:51

如果其他已经订阅了parentChanged $的东西,那么除了您未进入的副作用外,Pipe ShareReplay(1)在其结束时。

然后,只要parentChanged $已经订阅,当孩子组件或其他任何内容订阅时,它最初将获得最后一个发射的值 仍将执行一次,而不是每个订户。

如果parentChanged $尚未订阅,那么您要么必须始终订阅并执行上述操作,要么更改为“更改xyz.valuechanges”的模式。 concat(defer(()=&gt; of(xyz.value)),xyz.valuechanges),以便在订阅时,它以您要发送的初始值开始,然后/em>发送更改。

If something else is already subscribed to parentChanged$ then, in addition to the side effects you didn't go into, pipe shareReplay(1) at the end of it.

Then, as long as parentChanged$ is already subscribed to, when the child component or anything else subscribes to it, it will initially get the last-emitted value and any side effects will still be executed once, not once per subscriber.

If parentChanged$ isn't already subscribed to then you either have to always subscribe and do the above, or change to a pattern where you do something like changing xyz.valueChanges to concat(defer(() => of(xyz.value)), xyz.valueChanges) so that when you subscribe, it starts with the initial value you want to send, then sends changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文