Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference to global variable from a module

I have a global variable declared in the html header and want to reference it from a class inside a module. How can I prevent compiler error:

error TS2095: Could not find symbol 'selfGlobal'.

<html>
    <head>
        <script>
        var selfGlobal = this;
        var globalVariable = 1;
        </script>
    </head>
    <body>   
    <script src="test.js"></script>
    </body>
</html>

In test.ts

module Test{
    export class TestClass {
        private _privateVariable:any; 
        constructor() {
            this._privateVariable = selfGlobal.globalVariable; // compile error throws here, but the code can run

        }
    }
}

Thanks! Mars

like image 264
Mars Zhu Avatar asked Jul 20 '13 16:07

Mars Zhu


People also ask

How can you use a global name from one module in different module?

Just import the config module in all modules of your application; the module then becomes available as a global name.

How do you reference a global variable in Java?

To define a Global variable in java, the keyword static is used. Java actually doesn't have the concept of Global variable, it is known as class variable ( static field ). These are the variables that can be used by the entire class. // constructer used to initialise a Student object.


1 Answers

You need to tell the compiler it has been declared:

declare var selfGlobal: any;
like image 84
mak Avatar answered Sep 20 '22 01:09

mak