Search code examples
angularng-bootstrap

How should I initialize ngbDatepicker with formControlName instead of [(ngmodel)]


I need to use ReactiveForms, with [formGroup] and formGroupName="field"

<form [formGroup]="generalForm" (ngSubmit)="onSubmit()">
  <input class="form-control" placeholder="yyyy-mm-dd" 
  formControlName="dateIni" ngbDatepicker #a="ngbDatepicker">
</form>

Component.ts

generalForm: FormGroup;

ngOnInit() {
this.generalForm = this.formBuilder.group({
  name: ['', Validators.required],
  dateIni: ['', Validators.required],
  dateFin: ['', Validators.required],
  registerDateLimit: ['', Validators.required],
});

}

In my code, I tried to put a default value:

public dateIni: { year: 2017, month: 8, day: 8 };

or

@Input() dateIni: { year: 2017, month: 8, day: 8 };

but it is not taking the default value and all the docs only mention the case with template forms.

Any Idea how should I do it ?


Solution

  •   dateIni: ['', Validators.required],
    

    you need to put your initial value here like

     dateIni: ['2014-01-01', Validators.required],
    

    in correct format