Angular Mat Calendar Disable date based on Rest result

感情迁移 提交于 2020-03-02 07:49:25

问题


I am using Angular material calendar (i.e) mat-calendar. I am trying to disable some of the dates in the calendar based on dynamics value.

HTML

    <mat-calendar [headerComponent]="exampleHeader" [dateFilter]="filterDates"></mat-calendar>

I am using a custom header, to catch next/previous month events and then call some rest api, and based on the result of this call, I want to disable some dates. Calls of this.nextClicked() and this.previousClicked(); are generated only after the rest api gave response.

I am using the dateFilter attribute, but all my variables are undefined

TS

  public filterDates(date: Date): boolean {
    if (this.apiResult !== undefined) {
      let d = this.pipe.transform(date, 'yyyy-MM-dd');
      return this.apiResult.has(d);
    } else {
      return true;
    }
 }

If you have any idea or any methods to change disabled dates programmatically Thanks


回答1:


To take the context into your filterDates method you need to use arrow functions instead of declaring it as a regular method as you did (otherwise the this keyword will be referring to the context inside matDateFilter class, where your pipe and apiResult don't exist) :

filterDates = (date: Date): boolean => {
  if (this.apiResult !== undefined) {
    let d = this.pipe.transform(date, 'yyyy-MM-dd');
    return this.apiResult.has(d);
  } else {
    return true;
  }
}

Stackblitz demo



来源:https://stackoverflow.com/questions/57978516/angular-mat-calendar-disable-date-based-on-rest-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!