I have a component class as EventSchedulePage.It extends HandleStorageService abstract class as shown below.You can see that there is
hmm.. It seems anti-pattern no? I mean using service layer to handle UI related things? That is why I tried to get the solution based on TS.What is your thoughts about this? – Sampath
It is definitely more MVCS-like (Model-View-Controller-Service) to handle that in the controller. But that is a widely taken approach.
If you want to go for it, @suraj's answer is my personal recommendation.
Handling alerts on the controller is certainly possible. Keep reading.
event-schedule-page.service.ts
export class EventSchedulePage extends HandleStorageService {
// ...
foo() {
if (!bar) {
throw new Error('Something went wrong.');
}
// ...
}
}
home.controller.ts
export class HomeController {
// ...
foo() {
try {
eventSchedulePageService.foo();
} catch (error) {
window.alert(error); // Handle and UI display the error on the controller.
}
}
}
To follow up, you can use custom error classes or separate functions to throw / handle your errors.