angular2 mydatepicker无法正常工作

时间:2017-05-25 12:19:50

标签: angular

我正在使用角度2 mydatepicker。

我需要默认选择值。

这是我的代码

import {IMyOptions,IMyDateModel} from 'mydatepicker';
import { FormGroup, FormBuilder, Validators ,FormControl} from '@angular/forms';

export class CommentsPage {
Addevent;
private myDatePickerOptions: IMyOptions = {
  dateFormat: 'yyyy-mm-dd',
};
constructor() {
  let date = new Date();
  this.Addevent.controls['EVENT_DATE'].setValue({
    date: {
         year: date.getFullYear(),
         month: date.getMonth() + 1,
         day: date.getDate()}
  });
}

ngOnInit() 
    {

      this.Addevent = this.formbuilder.group({
       EVENT_DATE:[''],
      }); 
    } 
}  

这是我的Html代码

<form [formGroup]="Addevent" (ngSubmit)="add(Addevent.value)" novalidate>
       <div>    
        <h5 class="d1">DATE</h5>
        <my-date-picker name="mydate" [options]="myDatePickerOptions"
                   formControlName="EVENT_DATE"></my-date-picker>
        </div>  
 </form>

但我得到了这个错误

Error: There is no FormControl instance attached to form control element with name: 'EVENT_DATE'

我该如何解决这个问题。

请建议我,

感谢。

1 个答案:

答案 0 :(得分:0)

我没有测试它。但是从错误中,这可以通过将FormControl实例设置为Addevent来帮助您。

import {IMyOptions,IMyDateModel} from 'mydatepicker';
import { FormGroup, FormBuilder, Validators ,FormControl} from '@angular/forms';


export class CommentsPage {
Addevent: FormGroup;
private myDatePickerOptions: IMyOptions = {
  dateFormat: 'yyyy-mm-dd',
};
constructor(private formbuilder: FormBuilder) {
  let date = new Date();
  this.Addevent.controls['EVENT_DATE'].setValue({
    date: {
         year: date.getFullYear(),
         month: date.getMonth() + 1,
         day: date.getDate()}
  });
}

ngOnInit() 
    {

      this.Addevent = this.formbuilder.group({
       EVENT_DATE:[''],
      }); 
    } 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>