mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
5fb7ae20a2
initialize the events fields of the EventsComponent so that they are always defined PR Close #47458
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
|
|
import { EventsService } from './events.service';
|
|
|
|
const DAY = 24 * 60 * 60 * 1000;
|
|
|
|
export interface AngularEvent {
|
|
name: string;
|
|
linkUrl?: string;
|
|
date: {
|
|
start: `${number}-${number}-${number}`; // Date string in the format: `YYYY-MM-DD`
|
|
};
|
|
}
|
|
|
|
@Component({
|
|
selector: 'aio-events',
|
|
templateUrl: 'events.component.html'
|
|
})
|
|
export class EventsComponent implements OnInit {
|
|
|
|
pastEvents: AngularEvent[] = [];
|
|
upcomingEvents: AngularEvent[] = [];
|
|
|
|
constructor(private eventsService: EventsService) { }
|
|
|
|
ngOnInit() {
|
|
this.eventsService.events.subscribe(events => {
|
|
this.pastEvents = events
|
|
.filter(event => isInThePast(event))
|
|
.sort((l: AngularEvent, r: AngularEvent) => (l.date.start < r.date.start) ? 1 : -1);
|
|
|
|
this.upcomingEvents = events
|
|
.filter(event => !isInThePast(event))
|
|
.sort((l: AngularEvent, r: AngularEvent) => (l.date.start < r.date.start) ? -1 : 1);
|
|
});
|
|
}
|
|
}
|
|
|
|
function isInThePast(event: AngularEvent): boolean {
|
|
return new Date(event.date.start).getTime() < Date.now() - DAY;
|
|
}
|