How disable all the dates before a particular date in angular?

前端 未结 6 564
清酒与你
清酒与你 2021-02-07 20:32

I am building a component (html, css, spec.ts, ts) in angular in which I always want endDate > startDate. I have followed this link https://material.angular.io/components/datepi

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-07 21:03

    Since you are using a reactive form, utilize the form controls. It's not recommended to have two bindings (ngModel and formControl). So drop the ngModel like I suggested in a previous question of yours: https://stackoverflow.com/a/47426879/6294072

    So populate your form controls with the values of from your object unavailability.

    constructor(private formBuilder: FormBuilder) { 
      this.unavailabilityForm = this.formBuilder.group({
        startDate: [this.unavailability.startDate],
        endDate: [this.unavailability.endDate]
      });  
    }
    

    if you are receiving the values at a later point you can use patchValues:

    this.unavailabilityForm.setValue({
      startDate: this.unavailability.startDate;
      endDate: this.unavailability.endDate;
    })
    

    else you can set the values when you build the form.

    Then the only thing you need to add to your second datepicker is [min] like the other answer mentioned. There utilize the form control value:

    
    

    DEMO

提交回复
热议问题