Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 (global variables): How do I access a variable defined in index.html, in the Typescript code?

I have this code:

 <script src="node_modules/nedb/browser-version/out/nedb.min.js"> </script>

 <script>
 var server = new Nedb({filename: 'someFile2', autoload: true});
 </script>

The Nedb persistent database is created.

I just want to access the server variable in the app code, like in MyCmp.ts. How do I do this?

like image 338
coder Avatar asked Oct 23 '25 07:10

coder


1 Answers

You need to declare Nedb inside Mycmp, then you should call the initialization code.

declare var Nedb: any;

@Component(...)
export class MyCmp {

    server: any;

    ngOnInit() {
       this.server = new Nedb({filename: 'someFile2', autoload: true});
    }
}

Now you can access this.server inside other functions inside Mycmp.

like image 58
Alexander Ciesielski Avatar answered Oct 24 '25 21:10

Alexander Ciesielski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!