There are various of registering a class as a dependency in angular.
1. Registering globally to root [This will be available to entire application]:
LocalServicesService .service.ts
@Injectable({
providedIn: 'root'
})
export class LocalServicesService {
getDetails (){}
}
Now if the class is registered as injectable then we can use this class any where in the entire solution as dependency class for ex like this :
HeaderComponent.ts
@Component({
selector: '',
templateUrl: '',
providers: []
})
export class HeaderComponent implements OnInit {
constructor(private service : LocalServicesService) { }
ngOnInit() {
this.service.getDetails().subscribe();
}
}
2. Registering service to specific component
LocalServicesService .service.ts
@Injectable()
export class LocalServicesService {
getDetails (){}
}
HeaderComponent.ts
@Component({
selector: '',
templateUrl: '',
providers: [LocalServicesService]
})
export class HeaderComponent implements OnInit {
constructor(private service : LocalServicesService) { }
ngOnInit() {
this.service.getDetails().subscribe();
}
}
1. Registering globally to root [This will be available to entire application]:
LocalServicesService .service.ts
@Injectable({
providedIn: 'root'
})
export class LocalServicesService {
getDetails (){}
}
Now if the class is registered as injectable then we can use this class any where in the entire solution as dependency class for ex like this :
HeaderComponent.ts
@Component({
selector: '',
templateUrl: '',
providers: []
})
export class HeaderComponent implements OnInit {
constructor(private service : LocalServicesService) { }
ngOnInit() {
this.service.getDetails().subscribe();
}
}
2. Registering service to specific component
LocalServicesService .service.ts
@Injectable()
export class LocalServicesService {
getDetails (){}
}
HeaderComponent.ts
@Component({
selector: '',
templateUrl: '',
providers: [LocalServicesService]
})
export class HeaderComponent implements OnInit {
constructor(private service : LocalServicesService) { }
ngOnInit() {
this.service.getDetails().subscribe();
}
}
In this scenario the LocalServices class can be injectable to only HeaderComponent