无法使用typescript访问angular中另一个控制器中声明的$rootscope属性

unable to access $rootscope property declared in one controller in other in angular using typescript

本文关键字:声明 控制器 属性 rootscope 另一个 typescript 访问 angular      更新时间:2024-02-19

我在如下所示的ts文件中声明了一个rootscope自定义属性

module MyApp.Entity{
    export interface IRootScope extends ng.IRootScopeService {
        userData: any;
        clientData: any;
    }
} 

在我的一个服务文件中,我分配了值

    export class Service1 {
            constructor(private apiService: MyApp.Common.Services.apiService, private notificationService: MyApp.Common.Services.notificationService,
                 private $base64: JQueryBase64Static, private $cookieStore: ng.cookies.ICookieStoreService, private $rootScope: MyApp.Entity.IRootScope) {
            }
    saveData(){
    this.$rootScope.clientData = clientData; // Data got assigned 

    }
}

现在在其他控制器文件中,我试图访问这个根范围属性

class controller2{
Clients: any;
constructor(private service1: MyApp.Service.Service1, private $rootScope: MyApp.Entity.IRootScope){
this.Clients = this.$rootScope.clientData; // clientData is Undefined i.e here //it is creating new rootscope again I guess ..so How to access a single global //rootscope
}
  }

您使用的是this.$rootScope,它是$rootScope的另一个实例,因为它只在当前作用域中定义,如果您删除this.,它应该可以在中工作