如何在Angular 4的ngbDatepicker中禁用上一个日期? [英] How to disable previous date in ngbDatepicker in Angular 4?

查看:71
本文介绍了如何在Angular 4的ngbDatepicker中禁用上一个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁用ngbDatepicker中的所有以前/过去的日期, 我用过ngbDatepicker.
我的HTML是:

I want to disable all the previous/past date in ngbDatepicker, I have used ngbDatepicker.
My HTML is:

<input required class="form-control" placeholder="dd-MM-YYYY" name="startDate" required ngbDatepicker #d="ngbDatepicker"
            [readonly]="true" [formControl]="formModel.controls.startDate" [ngClass]="{'has-error':!formModel.controls['startDate'].valid && formModel.controls['startDate'].touched}" />

推荐答案

抱歉,迟到了.我刚刚遇到了这个问题,上面的答案使NgbDatcConfig的属性发生了变化.该解决方案可以正常工作,但是会影响所有其他NgbDatepicker实例.推荐的方法是利用

Sorry for being late. I just came across this question, and the above answer that mutates the properties of NgbDatcConfig. That solution works fine, but it will affect all the other NgbDatepicker instances. The recommended way to do so would be to make use of the [minDate] input binding as stated on the API.

您可以在component.html上以这种方式使用它,

You can use it this way on your component.html,

 <input class="form-control" ngbDatepicker [minDate]="minDate" (click)="datePicker.toggle()" #datePicker="ngbDatepicker"" placeholder="yyyy-mm-dd">

在component.ts上,您可以按以下方式定义minDate:

And on your component.ts, you can define minDate in this manner:

minDate = undefined;
.
. 
constructor(private config: NgbDatepickerConfig) {
  const current = new Date();
  this.minDate = {
    year: current.getFullYear(),
    month: current.getMonth() + 1,
    day: current.getDate()
  };
}

这篇关于如何在Angular 4的ngbDatepicker中禁用上一个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆