我正在尝试重置 primeng datepick 日历,但不起作用。我正在 Angular 8 中工作。我使用 primeng 创建了自定义 datepicker 组件。我在下面给出了我的代码。如何重置它?任何人都可以有想法吗?请帮助寻找解决方案。
app.component.html:
<p-calendar dateformat="mm/dd/yyyy" [numberofmonths]="4" selectionmode="range" formControlName="datePick"></p-calendar>
<button (click)="resetdate()">Reset Date</button>
app.component.ts:
@ViewChild('p-calendar') calendar: any;
resetdate(){
this.calendar.value = null;
}
最佳答案
您正在使用响应式(Reactive)表单,在响应式(Reactive)表单中它将使用表单控件 setValue 方法进行设置。
更新:您可以以数组形式提供模型值。
组件.html
<hello name="{{ name }}"></hello>
<div [formGroup]="myGroup">
<p>
Date Reset
</p>
<p-calendar formControlName="date" selectionMode="range" [readonlyInput]="true"></p-calendar>
<button (click)="reset()">Reset</button>
<button (click)="setCustomDateRange()">Set Custom Date Range</button>
</div>
组件.ts
import { Component } from '@angular/core';
import { FormControl , FormGroup} from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
yearRange = '2000:2020';
myGroup: any;
rangeDates: Date[];
constructor(){
this.myGroup = new FormGroup({
date: new FormControl('')
});
}
setCustomDateRange(){
var date = new Date();
date.setDate(date.getDate() + 10);
this.myGroup.get('date').setValue([new Date(), date]);
}
reset(){
console.log(this.myGroup.get('date').value)
this.myGroup.get('date').setValue(null);
}
}
关于angular7 - 如何重置 primeng 日期选择器日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58856932/