1

I am trying to pass some objects to a new browser window. I followed the suggestion from AngularJS: open a new browser window, yet still retain scope and controller, and services

It works on Chrome, but doesn't on IE. My shared objects are always undefined on IE. Any suggestions?

Code for simplified version of what I am trying to do

My parent html

<html ng-app="SampleAngularApp">   

<body>
 <div ng-controller="popupCtrl">
        <my-popup foo="foo" abc="abc">Open Popup from here</my-popup>
    </div>
</body>
</html>

My parent JS

var SampleAngularApp = angular.module('SampleAngularApp', []);

    var popupCtrl = function ($scope) {

        $scope.foo = { baz: 'qux' };
        $scope.abc = "12345";
    };

SampleAngularApp.directive('myPopup', ['$window', function ($window) {
    return {
        restrict: 'EA',
        scope: {
            foo: '=',
            abc: '='
        },
        link: function (scope, elem, attrs) {
            elem.css({ 'cursor': 'pointer' });
            elem.bind('click', function () {

            var popWdw = $window.open("popupWindow.html", "popupWindow", "width=500,height=500,left=100,top=100,location=no");

            popWdw.abc = scope.abc;
            popWdw.foo = JSON.stringify(scope.foo);

            });
        }
    };
}]);

My popup html

<html ng-app="PopupApp">

<body ng-controller="childCtrl">

</body>
</html>

My popup JS

var PopupApp = angular.module('PopupApp', []);

var childCtrl = function ($scope) {   

    alert(window.foo);

};

PopupApp.controller(childCtrl);
Community
  • 1
  • 1
Young.Programmer
  • 153
  • 1
  • 3
  • 8
  • Think you need to look into the problem from a JavaScript perspective first then worry about applying it to Angular. The underlying issue you're encountering sounds like it's just a problem with the JS itself, without using angular test to see if in a simple case you can pass any information from one window to another. http://stackoverflow.com/questions/87359/can-i-pass-a-javascript-variable-to-another-browser-window – shaunhusain Apr 07 '14 at 19:45
  • @ shaunhusain : As you said the problem could be with the JS itself, but I am unable to figure it out. When I try with just 2 html pages and no angular, everything just works fine. Can you please look at my above code and let me know what I am missing. Though I didn't post references, they are all properly referenced in my solution as it works on Chrome. Thanks! – Young.Programmer Apr 07 '14 at 23:00
  • Perhaps its a timing issue. Try using $timeout or add a button with an ng-click handler to the pop up app, so you can query the value of `window.foo`. Perhaps `window.foo` gets populated after your pop up app's controller is initialized. The link that shaunhusain added refers to using the `window.opener` property (from the popup). You could try the opposite and set `window.foo` in the parent app and then use `window.opener.foo` to access the value from the pop up... – Sunil D. Apr 08 '14 at 00:44
  • Thanks shaunhusain and SunilD. Setting the parent property and using the window.opener worked. I am still unsure why it doesn't work the other way around. – Young.Programmer Apr 08 '14 at 11:30

1 Answers1

5

Per shaunhusain and Sunil D's suggestions, I have changed my code as below and it works

My parent JS

link: function (scope, elem, attrs) {
            elem.css({ 'cursor': 'pointer' });
            elem.bind('click', function () {

            $window.abc = scope.abc;
            $window.foo = JSON.stringify(scope.foo);

            var popWdw = $window.open("popupWindow.html", "popupWindow", "width=500,height=500,left=100,top=100,location=no");            

            });
        }

My popup JS

var childCtrl = function ($scope) {   

    alert(window.opener.foo);

};
Young.Programmer
  • 153
  • 1
  • 3
  • 8
  • The above solution works can i know how to take back the value from child to parent here's my post http://stackoverflow.com/questions/24659514/how-to-exchange-a-value-from-pop-up-to-parent-window-in-angularjs?noredirect=1#comment38229609_24659514 – Arun Jul 10 '14 at 03:38