如何在订阅之前获得形式控制的ValueChanges值?
嗨,我有父母表格控制和一个子组件。在父级控件发生更改之前,不显示子组件,例如:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果其他已经订阅了
parentChanged $
的东西,那么除了您未进入的副作用外,PipeShareReplay(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, pipeshareReplay(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 changingxyz.valueChanges
toconcat(defer(() => of(xyz.value)), xyz.valueChanges)
so that when you subscribe, it starts with the initial value you want to send, then sends changes.