Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 put a global constant available to zone.js

Tags:

angular

zone

i am using Angular (4 i think) with typescript and zone.js (0.8.4). I import zone.js via the "polyfills.ts" file. When I look inside the source code of zone.js, there is code like this:

var isDisableIECheck = _global['__Zone_disable_IE_check'] || false;

My question is, how can I set this variable in _globals ?

Thanks

like image 570
Jojje Avatar asked Aug 15 '17 11:08

Jojje


People also ask

Can you have global variables in angular?

We always need to declare global variable file for our angular 8 application because we can set some global variable there like site title, api url etc so you can easily access any where in our application easily. So, in this example, we will create GlobalConstants file for adding global variables in our application.

Does angular Use Zone js?

js provides a mechanism, called zones, for encapsulating and intercepting asynchronous activities in the browser (e.g. setTimeout , , promises). These zones are execution contexts that allow Angular to track the start and completion of asynchronous activities and perform tasks as required (e.g. change detection).

What is Zone js for?

js is a way of creating a new Context and getting the code executed within that context/Zone. Any code enclosed within a Zone whether synchronous or asynchronous is executed within the same context.


1 Answers

global is window object in a browser as can be seen here:

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
    typeof define === 'function' && define.amd ? define(factory) :
    (factory());
}(this,    <------------ `this` points to `window` in global scope
 (function () { 
   ...
});

so you can set the variable like this:

window['__Zone_disable_IE_check'] = true;

But you need to do that before zone.js is loaded. If you load zone.js in index.html, add the following:

<script>
    window['__Zone_disable_IE_check'] = true;
</script>
<script src="node_modules/zone.js/dist/zone.js"></script>
like image 105
Max Koretskyi Avatar answered Oct 05 '22 17:10

Max Koretskyi