2013-07-20 11 views
6

Ho una variabile globale dichiarata nell'intestazione html e voglio farvi riferimento da una classe all'interno di un modulo. Come posso evitare l'errore del compilatore:Riferimento alla variabile globale da un modulo

errore TS2095: Impossibile trovare il simbolo '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 

     } 
    } 
} 

Grazie! Mars

+1

Vedi anche http://stackoverflow.com/questions/13252225/call-a-global-variable-inside-typescript-module – koppor

risposta

9

devi dire al compilatore è stato dichiarato:

declare var selfGlobal: any; 
+1

Potrebbe anche essere 'dichiarare var selfGlobal: Window;' :) – basarat

+1

Grazie funziona. Ho un errore del compilatore contro GlobalVariable, ma scompare dopo aver dichiarato anche GlobalVariable. –

Problemi correlati