0

I'm trying to display a list of dates for the current week in an Angular app. I want to allow users to view previous weeks at the click of a button, so I'm using an Observable to update the array of dates, and attempting to display the updated array.

All items are updated in the view, except for the first item in the array. Plunker example here

I've tried using *ngFor and the async pipe, as well as explicitly creating elements for each item in the array (like below). Both have the same issue. I'm struggling to find a solution.

//our root app component
import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';

@Component({
  selector: 'my-app',
  template: `
    <button (click)="previousWeek()">Prev Week</button>
    <div>{{dates[0]}}</div>
    <div>{{dates[1]}}</div>
    <div>{{dates[2]}}</div>
  `,
})
export class App {
  name:string;
  dates: Date[];
  public $datesSource: Observable<Date[]>;
  private datesSource: Subject<Date[]>;

  constructor() {
    this.datesSource = new Subject<Date[]>();
    this.datesSource$ = this.getDatesWithObservable();
    this.datesSource$.subscribe((dates) => {
      console.log(dates);

      this.dates = dates;
    })

    this.setDates(new Date());
  }

  setMonday(date: Date): Date {
        const day = date.getDay() || 7;
        if (day !== 1) {
            date.setHours(-24 * (day - 1));
        }
        return date;
    }

  setDates(date: Date): void {
        const dates = [
            new Date(),
            new Date(),
            new Date(),
            new Date(),
            new Date(),
            new Date(),
            new Date()
        ];
        const monday = this.setMonday(date);
        dates[0] = monday;
        const mondayDate = monday.getTime();
        dates.forEach((date, idx) => {
            console.log(idx);

            date.setTime(monday.getTime() + (idx * 24 * 60 * 60 * 1000));
    });
        this.addDates(dates);
    }

    addDates(dates: Date[]): void {
        this.datesSource.next(dates);
    }

    getDatesWithObservable(): Observable<Date[]> {
        return this.datesSource.asObservable();
    }


    previousWeek(): void {
      const day = this.dates[0].getDay() || 7;
      const lastWeek = this.dates[0];
      const days = 7;
      lastWeek.setTime(lastWeek.getTime() - (days * 24 * 60 * 60 * 1000));
      this.setDates(lastWeek);
    }
}

1 Answer 1

1

try this , i commented the line in the middle and it's working, can you check:

     const monday = this.setMonday(date);
     //dates[0] = monday;
     const mondayDate = monday.getTime();
Sign up to request clarification or add additional context in comments.

1 Comment

Do you think this has something to do with Angular change detection? Because with that line in there the app actually updates dates[0] twice, and maybe Angular only recognizes the first change?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.