I am building a web App and I created a 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;
}
}
When I inject this in a component I have the following error:
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!
When I remove the attributes from the constructor it works
export class ModuleService {
startTime = 8;
endTime = 12;
constructor() { }
I read here https://angular.io/guide/dependency-injection#non-class-dependencies that I have to inject an InjectionToken when it's a non-class dependencie (a number in my case). So my question is which is the best way, is it by creating an injectionToken or by declaring the attribute as I did above?