Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ngbDatePicker is only capturing the previous value when [ngModelOptions]="{ updateOn: 'blur' }" #2976

Closed
ahmadakra opened this issue Jan 18, 2019 · 2 comments

Comments

@ahmadakra
Copy link

ahmadakra commented Jan 18, 2019

Bug description:

Consider this standard snippet of code:

    <div class="input-group" *ngIf="isEdit">
      <input class="form-control" placeholder="yyyy-mm-dd" ngbDatepicker #d="ngbDatepicker" [(ngModel)]="model.BirthDateTime" [ngModelOptions]="{ updateOn: 'blur' }">
      <button class="btn btn-primary" (click)="d.toggle()" type="button">
        <fa-icon icon="calendar-alt"></fa-icon>
      </button>
    </div>

It creates a datepicker inside a popup, this is what it looks like with my custom styling:
image

When you set the following option: [ngModelOptions]="{ updateOn: 'blur' }", ngbDatepicker does not behave correctly when you select a date from the popup, the value of the model is not the latest value you select, but the previous one selected just before it. And the slightly sinister part is that the latest value you just selected appears correctly in the input field as you would expect, so you would have no clue that the value of the model underneath is different.

Upon investigation it seems to be an issue with how ngbDatepicker implements ControlValueAccessor. When you select a date from inside the popup, ngbDatepicker fires the "touched" event BEFORE it fires the "change" event, and therefore upon the "touched" event angular updates the model to the old value before the new value has been captured with the "change" event. If that makes sense.

Versions:

Angular: 7.0.0

ng-bootstrap: 4.0.1

Bootstrap: 4.2.1

@ahmadakra ahmadakra changed the title ngbDatePIcker is only capturing the previous value when [ngModelOptions]="{ updateOn: 'blur' }" ngbDatePicker is only capturing the previous value when [ngModelOptions]="{ updateOn: 'blur' }" Jan 18, 2019
@maxokorokov
Copy link
Member

Could you please provide a stackblitz with the minimal reproduction scenario?
You could fork this one, for example: https://ng-bootstrap.github.io/#/components/datepicker/examples#popup

@ahmadakra
Copy link
Author

@maxokorokov here you go:
https://stackblitz.com/edit/angular-3pwuvv

I literally just added [ngModelOptions]="{ updateOn: 'blur' }" in the sample you referenced.
Best wishes,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants