这个问题通常出现在使用p-calendar组件时使用响应式表单和patchValue方法时。它的解决方法是使用setValue方法,而不是patchValue方法。以下是一个示例:
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'app-example',
template: `
`
})
export class ExampleComponent implements OnInit {
form: FormGroup;
ngOnInit() {
this.form = new FormGroup({
date: new FormControl(null)
});
this.form.setValue({
date: new Date()
});
}
}
注意,setValue方法需要一个对象作为参数,这个对象的键应该与表单控件的名称相同,也需要确保它们的值是适合p-calendar组件的。