无法使用打字稿访问在一个控制器中声明的$ rootscope属性在另一个控制器中声明的角度 [英] unable to access $rootscope property declared in one controller in other in angular using typescript

查看:81
本文介绍了无法使用打字稿访问在一个控制器中声明的$ rootscope属性在另一个控制器中声明的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have declared a rootscope custom property in a ts file like below

module MyApp.Entity{

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

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

In one of my service file I have assigned value

    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 


    }
}

现在在其他Controller文件中,我正尝试访问该rootscope属性

Now in other Controller file I was trying to access this rootscope property

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.后,它才在当前作用域中定义

You are using this.$rootScope which is a different instance of $rootScope since it is only defined in your current scope if you remove the this. it should work

这篇关于无法使用打字稿访问在一个控制器中声明的$ rootscope属性在另一个控制器中声明的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆