Angular项目使用EJS-Schedule报错:Cannot read property 'trim' of undefined
That error pops up because the Schedule component isn’t getting the necessary setup to properly format date ranges when rendering the week view. Let’s walk through the fixes step by step:
1. Register all required view services
The EJ2 Schedule relies on specific services to render different views (day, week, month, etc.). Even if you’re just using the default week view, you need to provide these services in your module to avoid initialization gaps.
Update your app.module.ts to include the required services:
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { ScheduleModule, DayService, WeekService, WorkWeekService, MonthService, AgendaService } from '@syncfusion/ej2-angular-schedule'; import { AppComponent } from './app.component'; @NgModule({ imports: [BrowserModule, ScheduleModule], declarations: [AppComponent], // Add these core view services to the providers array providers: [DayService, WeekService, WorkWeekService, MonthService, AgendaService], bootstrap: [AppComponent] }) export class AppModule { }
2. Set an initial currentDate for the component
Without a defined starting date, the component struggles to compute the date range for the header, leading to that undefined value that triggers the trim error.
Update your component’s TS file:
import { Component, ViewChild } from '@angular/core'; import { ScheduleComponent } from '@syncfusion/ej2-angular-schedule'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { @ViewChild('scheduleObj', {static: true}) public schedule: ScheduleComponent; // Define an initial current date to anchor the schedule public currentDate: Date = new Date(); }
Then bind this property in your HTML template:
<ejs-schedule #scheduleObj [currentDate]="currentDate"></ejs-schedule>
3. Verify version compatibility
Double-check that your @syncfusion/ej2-angular-schedule version matches your Angular version. Mismatched versions can cause unexpected initialization bugs. You can cross-reference compatible versions in Syncfusion’s official version matrix.
After applying these steps, the component should initialize properly without the "trim of undefined" error.
内容的提问来源于stack exchange,提问作者medi

