问题描述
我想禁用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
实例.推荐的方法是利用 API .
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中禁用上一个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!