I'm in the process of developing a web App and recently put together a new service:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ModuleService {
constructor(private startTime: number = 8, private endTime: number = 12) { }
get start() {
return this.startTime;
}
get end() {
return this.endTime;
}
set start(startTime) {
this.startTime = startTime;
}
set end(endTime) {
this.endTime = endTime;
}
}
However, when I try to inject this service into a component, an error surfaces:
ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[Number]:
StaticInjectorError(Platform: core)[Number]:
NullInjectorError: No provider for Number!
Error: StaticInjectorError(AppModule)[Number]:
StaticInjectorError(Platform: core)[Number]:
NullInjectorError: No provider for Number!
Strangely enough, if I eliminate the attributes from the constructor, everything seems to work fine:
export class ModuleService {
startTime = 8;
endTime = 12;
constructor() { }
After doing some research on https://angular.io/guide/dependency-injection#non-class-dependencies, it appears that when dealing with non-class dependencies (like numbers in my case), injecting an InjectionToken is recommended. So, now I am pondering between creating an injectionToken or simply declaring the attribute as shown above. Any thoughts on the best approach?