question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No output emitted when value changed to initial value set from Subject.next()

See original GitHub issue

So, I’m not sure if this is an issue with ngx-sub-form or my understanding of how to accomplish this. I want to be able to set the initial value of a select or radio button of an ngx-sub-form’s NgxAutomaticRootFormControl or NgxRootFormControl. I tried using the technique described on the blog https://dev.to/maxime1992/building-scalable-robust-and-type-safe-forms-with-angular-3nf9 where it mentions under spaceship-container.component.ts to use an rxjs Subject and call it’s .next() method. This does work, but then if I change a value of the select/radio and then try to go back to the initial choice, with the auto method I get no output and with the manual method I get the previous value instead. It happens whether the value is a primitive type (like string) or an object or array.

I’ve tried to simplify showing this off in the following StackBlitz: https://stackblitz.com/edit/ngx-sub-form-initial-next

The 4 forms are to show the different scenarios I figured would experience this problem, 2 with NgxAutomaticRootFormControl and 2 with NgxRootFormControl, each having a version for an array object as value and a string as value.

In app.component.ts, I have a variable called doInitialNext. If it is false, no initial value is set and even going back to the null value of each select still emits the correct output. But when set to true (as the example is set to), the value sent from ngAfterViewInit is not emitted either automatically or manually when it is selected again later.

One other thing to note is that overriding emitInitialValueOnInit to true in any of the form components has no effect on this.

If this isn’t a bug in ngx-sub-form, then I’d like to know what I’m doing wrong (and perhaps the blog post should be edited to not include that). I’ve dug a tad into the code, though, and it seems that NgxRootFormComponent’s dataInputUpdated is never called and the check in its onRegisterOnChangeHook against this.dataInput$.value is part of the problem. Perhaps I’m wrong, though.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
CyberBotXcommented, Sep 30, 2019

The reason I used [ngValue] instead of [value] is because the former allows for objects while the latter only allows strings. It’s a subtle difference. Kinda weird that it works fine with ngx-sub-form, though.

Although, making the change you suggested works on the array examples but not on the string examples. Those still don’t emit the new value when changing back to the one sent by .next().

0reactions
maxime1992commented, Sep 30, 2019

☝️ Already closed because turns out that ngValue does work correctly once the fix was applied on your example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

BehaviorSubject .next() is not emitting updated value
1 Answer 1 · A BehaviorSubject is a multicasting observable and you should always keep it private to minimize the scope of calling...
Read more >
Understanding RxJS Subjects and their use - Tevpro
Subject has no initial value and does not return the current value on Subscription. It triggers only on .next(value) call and return/output the...
Read more >
combineLatest - Learn RxJS
Be aware that combineLatest will not emit an initial value until each observable emits at least one value. This is the same behavior...
Read more >
Observable | RxJS API Document - ReactiveX
Emits a given value if the source Observable completes without emitting any next value, otherwise mirrors the source Observable.
Read more >
5 helpful RxJS solutions. to everyday problems - Medium
These observables do not require an initial value, and combineLatest will therefore not emit anything until you have called next on them at ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found