Within a service class, I have initialized a $rootScope in the constructor and assigned a property to it using this.$rootScope. However, when attempting to access this property in another service within the same class, a new $rootScope is created and the property becomes undefined.
How can I resolve this issue? Is there a way to do this in Angular?
I previously asked this question on Stack Overflow.
EDIT:
export class DataService {
betterApproach = "value";
constructor(private $rootScope: any){
$rootScope.someProperty ="some value";
}
setBetterApproach(data){
this.betterApproach=data;
}
getBetterApproach() {
return this.betterApproach;
}
}
angular.module("services").service("dataService", ['$rootScope', DataService]);
class ConsumingService{
test:any;
constructor(private $rootScope: any ,
private dataService : DataService ){
this.test = $rootScope.someProperty;
this.test = dataService.getBetterApproach(); // Still returns old value
}
}
angular.module("services").service("ConsumingService", ['$rootScope', 'DataService',ConsumingService]);
When should I call setsetBetterApproach in DataController?
class DataCtrl{
constructor(private dataService: Dataservice){
}
myData(){
//this is ng-Click function
this.dataService.setBetterApproach("new Data");
}
}
I seem to be making some mistake as the value in betterApproach remains as 'value' instead of changing to 'newData'.