2017-05-25 82 views
0

我正在使用角度2 mydatepicker。angular2 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' 

我怎樣才能解決這個問題。

請告訴我,

謝謝。

回答

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>

+0

感謝UR reply.still我是得到同樣的犯錯.. – ANISUNDAR