In my Angular6 component, I am embedding an iframe. The javascript code inside the iframe needed me to declare a global object with a specific name ("API Callback") so that it can invoke different methods on the global object. Is it possible to create a global object like this as we used to do in JavaScript?
Asked
Active
Viewed 565 times
0
-
I think a global scope is one for the component. And if you want to make a scope that is the same over multiple you can put it in a shared service. – Swoox Sep 27 '18 at 10:01
-
Service wont work for me. The third party code which I embed in iframe needs me to create a global variable with few functions implemented. – Rajeev Sep 27 '18 at 10:03
-
Is the code within I frame in your control? – Ritesh Waghela Sep 27 '18 at 10:08
-
No, I cant change the code in iframe. – Rajeev Sep 27 '18 at 10:12
-
Why don't you create an object in window object.. in your app component? – Ritesh Waghela Sep 27 '18 at 10:16
1 Answers
0
If you want to create a global object, then just attach it to the window
object.
window.property = `I'm global`;
console.log(property);

Guerric P
- 30,447
- 6
- 48
- 86
-
thanks for the answer. I am using Angular6, so the below code worked for me. Got it from (https://stackoverflow.com/a/13480754/5841523) window['propertyName'] = MyTypeScriptCallbackObject; – Rajeev Sep 28 '18 at 02:51
-