Select Element valueChanges observable fires twice every time

See original GitHub issue

Version Alpha 46, TS

When trying to learn the various ways of working with forms I ran into an odd issue where select elements trigger change events twice for the same change when subscribed to the valueChanges observable.

If I have a function that changes the value from the model it only fires the change event once as expected.

It’s pretty short so heres my code:

@Component({
  selector: 'my-app',
})
@View({
  template: `
        <h3>Simple Select on App Component</h3>
        <form>
        <select [ng-form-control]="simpleSelect">
          <option value="1">One</option>
          <option value="2">Two</option>
          <option value="3">Three</option>
        </select>
        <button type="button" (click)="changeSelect()">Change Select to 3 From Model</button>
      </form>
  `,
  directives: [FORM_DIRECTIVES]

})
export class App {
  simpleSelect: Control = new Control('');
  counter: number = 0;
  constructor() {
    //watch simple select
    this.simpleSelect.valueChanges.subscribe((value)=>{
      console.log("Simple Select Val:" + value + " and counter:"+this.counter)
      this.counter++;
    });
  };
  changeSelect() {
    this.simpleSelect.updateValue(3);
  }
}

And Here is a Plunker of this in action.

I thought maybe it was something with how simple I was interfacing it so I did it with the FormBuilder and got the same results. I added a text field to see how it’s changes effect the form and they work correctly (only fire once) That Plunker Here

I’m not sure if I’m doing something incorrectly, but I think the expected behavior is the event to fire only once. I’m not experienced enough to track back whats causing the double bindings so I hoped I could have someone take a look at it…

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
manpuriasudipcommented, Jul 18, 2018

this issue still exists on mat-input

2reactions
ZeusKnightcommented, Aug 9, 2018

+1, this still exists in mat-input as of today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reactive forms valueChanges method fires twice for one ...
Try using distinctuntilchanged which is one of the rxjs operators. It will return unique values only. this ...
Read more >
Reactive Model Form • Angular - codecraft.tv
Both FormControls and FormGroups expose an observable called valuesChanged . By subscribing to this observable we can react in real-time to changing values ......
Read more >
RxJs Mapping: switchMap vs mergeMap vs concatMap vs ...
Instead, merge subscribes to every merged Observable at the same time, and then it outputs the values of each source Observable to the...
Read more >
Reactive Forms - setValue, patchValue And valueChanges
This property works like an observable as if any value or property changes in a form, it simply triggers that value – like...
Read more >
RxJS: The Unexpected - InDepth.Dev
valueChanges.pipe(startWith(null)));. If we are not sure about the default value, than we can try making sure the inner Observable fires before the source ......
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