简体   繁体   中英

Angular 2 pass JSON object to new window

I need to pass JSON object in new window which is opened using window.open()

I have requirement to display stored data in database into new window. At the same time i need to pass a JSON object which is available in current open window to this new window.

Possible ways I had :

  1. Can save json in cookie before open new window, and read it in ngOnInit() then remove it from cookie.
  2. Can pass required data in URL, but i have large JSON object

Please guide if we can do this by any another approach.

Thank you.

You can use localStorage too instead of cookies. I don't think you have better options if you have to use window.open() .

To keep each component functionality self-contained I would use a service to get the json based on the id.

Instead of cookies or localstorage, how about you use the window object you get when open the window.

var opened_window = window.open('someurl_here');
opened_window.json_data = JSON.stringify(this.local_json_data);

In the child window, you can access the data as part of your window object.

//Inject your window into this component (@Inject('Window') window maybe)
this.new_json_data = JSON.parse(this.window.json_data);

I think this should work.

I think you can use Window.postMessage . window.open will return a Window object that you can call postMessage on; in the new window, you'll want to listen for the "message" event on the global window object.

Note that IE8 and IE9 don't support it for windows, only (i)frames.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM