angular7 - 如何重置 primeng 日期选择器日历

标签 angular7 primeng angular8

我正在尝试重置 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);
  }


}

检查working code

关于angular7 - 如何重置 primeng 日期选择器日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58856932/

相关文章:

angular7 - 角料粘柱多于1柱

angular - Prime-ng 多选过滤器占位符不显示

javascript - Div 元素仅在点击后正确加载

angular - 如何使 PDF 适合高度 - ng2-pdf-viewer

Angular 8 UI observable 不触发更改检测 - ChangeDetectionStrategy 设置为默认值

angular8 - 升级到 Nebular 4 已停止显示 Font Awesome 图标。设置包也不起作用

angular7 - 未捕获( promise ): Error: Invalid CanActivate guard

angular - 如何使 Angular 垫表中的排序箭头始终可见?

javascript - 为什么使用 switchMap 时无法访问属性?

javascript - 使用 Angular 检测对 iframe 的点击