无法使用反应形式将my-date-picker输入初始化为当前日期

时间:2019-04-12 11:42:10

标签: angular datepicker reactive-forms

我正在使用MyDatePickerModule(https://github.com/kekeh/mydatepicker),并且想将日期选择器初始化为当前日期。我正在使用反应形式。

我遵循了这个https://github.com/kekeh/mydatepicker/wiki/Initialize-with-selDate-attribute

但它仍然无法正常工作。

这是模板部分:

module.exports = {
    getQuestionCounts: function(req, res){ // connected with Routes
        var arr = [];
        req.data.forEach(element => {
            module.exports.getCounts(element).then(function(count){
                console.log(count); // getting value
                element.count = count; 
                arr.push(element); // not updating with array
            });                  
        });           
        if(arr.length > 0){ // other operations }
    }
    getCounts: async function(data){
        var count = await Questions.countDocuments({"object":{$elemMatch: {data}}}).then(function(result){
            return result;
        });
        console.log(count); // getting count Fine
        return count;
    }
}

myComponent.ts:

<my-date-picker
  id="myDate"
  name="myDate"
  [options]="myDatePickerOptions"
  formControlName="myDate">
</my-date-picker>

以init形式的方法:

public todayDateToMyDatePicker(){
let d: Date = new Date();
return {year: d.getFullYear(),
  month: d.getMonth() + 1,
  day: d.getDate()};
 }

1 个答案:

答案 0 :(得分:0)

ngOnInIt() {
  this.updateValue();
}


updateValue() {
this.myForm.patchValue({
      myDate: this.todayDateToMyDatePicker()
    });
}
相关问题