【问题标题】:How to reset primeng datepick calender如何重置primeng datepicker日历
【发布时间】:2019-11-19 01:01:04
【问题描述】:

我正在尝试重置primeng datepick 日历但不工作。我正在使用angular 8。我已经使用primeng 创建了自定义日期选择器组件。我在下面给出了我的代码。如何重置它?任何人都可以有想法吗?请帮助找到解决办法。

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;
}

【问题讨论】:

    标签: angular7 primeng angular8


    【解决方案1】:

    您正在使用响应式表单,在响应式表单中它将使用表单控件 setValue 方法设置。

    更新:您可以将模型值作为数组提供。

    component.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

    【讨论】:

    • 工作中..谢谢
    • 如何设置日期范围的值?
    【解决方案2】:

    如下使用[(ngModel)]

    <p-calendar [(ngModel)]="value"></p-calendar>
    

    并在 ts 中使用:

     value: Date;
    
      resetdate() {
        this.value = null;
      }
    

    See working code

    【讨论】:

    • 告诉我更多帮助
    • 可以用formControlName吗?
    • 如果我这样使用不起作用:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多