0

I have a problem about retrieving some disabled dates in angular Datepicker,

when i am hard coding the dates i do not have problem, everything is working fine,

but when i retrieve the disabled dates from my web api backend, the disabled date are ignored ...

I'm still a noob in programming, i am probably missing something stupid

Is it something about async programming or observable variables ?

component.html

<input type="text" placeholder="Daterangepicker" class="form-control" bsDaterangepicker
      [datesDisabled]="disabledDates">

component.ts

export class Component implements OnInit {
  disabledDates :Date[];
  constructor(...) { }

  ngOnInit() {
      ...
      this.getUnavaibilities();
  }


  getUnavaibilities(){
    this.homesService.getUnavaibilities(this.Id).subscribe(
      reponse => this.UnavailabilitieshandleSuccesfulResponse(reponse));
  }
  UnavailabilitieshandleSuccesfulResponse(reponse){
    this.disabledDates = reponse;
    console.log(this.disabledDates) // this working fine !
   //output from console.log
   // Array(4) [ "2019-09-27T00:00:00", "2019-09-28T00:00:00", "2019-09- 
   //29T00:00:00", "2019-09-30T00:00:00" ]
  }
}

1 Answers1

0

Finnaly i found a solution, but i'm not sure about the quality of the solution If anybody got a better solution , I will be gratefull =)

I just made another var and put the result of the backend in it, After that I loop into the array of the response til the end and push every result into my main variable .

I'm prettry sure that's not the right way to do it but for the moment, i didn't found a better solution

export class Component implements OnInit {

 disabledDates :Date[] = [];
  test :Date[];
  constructor(...) { }

  ngOnInit() {

      ...
      this.getUnavaibilities();
  }


  getUnavaibilities(){
    this.homesService.getUnavaibilities(this.Id).subscribe(
      reponse => this.UnavailabilitieshandleSuccesfulResponse(reponse));
  }
  UnavailabilitieshandleSuccesfulResponse(reponse){
    this.test = reponse;
    console.log(this.test)
    for (var i = 0; i< this.test.length;i++) // for acts as a foreach
        {
            this.disabledDates.push(new Date(this.test[i]))
            console.log(this.test[i])

        }
  }
}