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

Timepicker doesn't initialize with ngModel #793

Closed
cyberbobjr opened this issue Jul 26, 2016 · 2 comments · Fixed by #2058
Closed

Timepicker doesn't initialize with ngModel #793

cyberbobjr opened this issue Jul 26, 2016 · 2 comments · Fixed by #2058

Comments

@cyberbobjr
Copy link

cyberbobjr commented Jul 26, 2016

Hi,
this is my code :

<timepicker [ngModel]="startTime" name="startTime" [hourStep]="1" [minuteStep]="15"
                                [showMeridian]="false"></timepicker>

the startTime variable is initialized inside the ngOnInit function with a specific DateTime object :

let momentTime = moment(this._event.startDateTime * 1000, 'x');
this.startTime = momentTime.toDate();

but the timepicker component never show the value of startTime ! only the the current time :(

Does someone have this problem too ?

@cyberbobjr cyberbobjr changed the title Timepicker don't initialize with ngModel Timepicker doesn't initialize with ngModel Jul 26, 2016
@ryanlangton
Copy link

ryanlangton commented Dec 30, 2016

Yes same problem here.

As a temporary hack, I've wrapped the TimePickerComponent so I can initialize the value in ngOnInit.

import { ControlValueAccessor } from '@angular/forms/src/directives';
import { TimepickerComponent } from 'ng2-bootstrap';
import { Component, OnInit, Input } from '@angular/core';

/**
 * The MyTimePickerComponent is a hack to get the time picker to initialize correctly from NgModel.
 * Do not change the template or underlying functionality
 * Once the bug in ng2-bootstrap is fixed this component can be removed (just use timepicker instead of sb-timepicker)
 * https://github.com/valor-software/ng2-bootstrap/issues/793
 * 
 * @export
 * @class MyTimepickerComponent
 */
@Component({
	selector: 'my-timepicker',
	template: `
    <table>
      <tbody>
        <tr class="text-center" [ngClass]="{hidden: !showSpinners || readonlyInput}">
          <td><a (click)="incrementHours()" [ngClass]="{disabled: noIncrementHours()}" class="btn btn-link"><span class="glyphicon glyphicon-chevron-up"></span></a></td>
          <td>&nbsp;</td>
          <td><a (click)="incrementMinutes()" [ngClass]="{disabled: noIncrementMinutes()}" class="btn btn-link"><span class="glyphicon glyphicon-chevron-up"></span></a></td>
          <td [ngClass]="{hidden: !showMeridian}" *ngIf="showMeridian"></td>
        </tr>
        <tr>
          <td class="form-group" [ngClass]="{'has-error': invalidHours}">
            <input style="width:50px;" type="text" [(ngModel)]="hours" (change)="updateHours()" class="form-control text-center" [readonly]="readonlyInput" (blur)="hoursOnBlur()" maxlength="2">
          </td>
          <td>:</td>
          <td class="form-group" [ngClass]="{'has-error': invalidMinutes}">
            <input style="width:50px;" type="text" [(ngModel)]="minutes" (change)="updateMinutes()" class="form-control text-center" [readonly]="readonlyInput" (blur)="minutesOnBlur()" maxlength="2">
          </td>
          <td [ngClass]="{hidden: !showMeridian}" *ngIf="showMeridian"><button type="button" [ngClass]="{disabled: noToggleMeridian() || readonlyInput}" class="btn btn-default text-center" (click)="toggleMeridian()">{{meridian}}</button></td>
        </tr>
        <tr class="text-center" [ngClass]="{hidden: !showSpinners || readonlyInput}">
          <td><a (click)="decrementHours()" [ngClass]="{disabled: noDecrementHours()}" class="btn btn-link"><span class="glyphicon glyphicon-chevron-down"></span></a></td>
          <td>&nbsp;</td>
          <td><a (click)="decrementMinutes()" [ngClass]="{disabled: noDecrementMinutes()}" class="btn btn-link"><span class="glyphicon glyphicon-chevron-down"></span></a></td>
          <td [ngClass]="{hidden: !showMeridian}" *ngIf="showMeridian"></td>
        </tr>
      </tbody>
    </table>
  `,
})
export class MyTimepickerComponent extends TimepickerComponent implements ControlValueAccessor, OnInit {
	@Input() initialTime: Date;

	ngOnInit() {
		super.ngOnInit();
		this.writeValue(this.initialTime);
	}
};

Usage:

        <my-timepicker [(ngModel)]="time" 
                    (change)="timeChanged($event)" 
                    [initialTime]="time"
                    [hourStep]="hourStep" 
                    [minuteStep]="minuteStep"
                    [showMeridian]="isMeridian">
        </my-timepicker>

@ryanlangton
Copy link

If you use my hack, be aware that the change event will no longer propagate to the parent. To get around this, I implemented AfterViewChecked in my component, do a comparison to the bound value, and perform onChanges actions if they're not equal.

@artemvfrolov artemvfrolov mentioned this issue Jun 15, 2017
valorkin added a commit that referenced this issue Jul 19, 2017
fixes #2036
fixes #1981 ( + min max demo )
fixes #1973
close #1957 ( + seconds demo )
fixes #1935
fixes #1672
feat #1007 added keyboard and mousewheel support
fixes #962
fixes #793
fixes #173
fixes #1271 added custom validation demo
fixes #1539 bs4
fixes #1253 if input is invalid

* feat(timepicker): new timepicker implementation

* feat(timepicker): new timepicker implementation testing

* chore(timepicker): removed old timepicker implementation

* chore(mini-ngrx): added ngrx licence

* fix(timepicker): fix seconds

* fix(timepicker): fix custom validation demo

* fix(tests): fix tests & aot errors

* fix(timepicker): min max restrictions

* fix(timepicker): min max checks

* feat(timepicker): add inputs validation (#2187)

* feat(timepicker): add inputs validation

* fix(timepicker): add isPM support

* feat(timepicker): add isValid output

* feat(timepicker): added test plan (#2127)

* fix(timepicker): fix ngModelChange demo

* fix(test): fix unit tests

* docs(timepicker): fix docs conflict, add isValid description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants