Reputation: 101
I need to pass the $scope from my parent window to new window. I coded $window.open() for new window in my javascript function. But in new window, I am not getting the $scope. Below I given the code.
1) JS File
$scope.mainPageNewsArchive = data.newsArchiveList;
var myWindow1 = $window.open("resources/partials/NewFile.html", "News Archive", 'toolbar=0,scrollbars=1,location=0,status=0,menubar=0,resizable=1,width=650, height=550,left = 300,top=100');
2) NewFile.html
<div>
<table border="0" cellspacing=0 cellpadding=2 width="100%" id="tkm">
<tbody>
<tr x-ng-repeat="newsArchive in mainPageNewsArchive track by $index">
<td align=left >{{newsArchive.createDate}}</td>
<td> </td>
<td align=left><a href="" ng-click="showNewsItem(newsArchive.id)" title="Click for more detail">{{newsArchive.title}}</a>
</td>
</tr>
</tbody>
</table>
</div>
Upvotes: 4
Views: 17236
Reputation: 192
You can get a copy of the parent scope by typing this in your parent controller:
$window.ScopeToShare = $scope;
and this in the controller used for your child window:
if($window.parent != null)
{
ParentScope = $window.opener.ScopeToShare;
}
Upvotes: 2
Reputation: 1035
A window which is opened by your main window will architecturally be unable to share the same scope with its parent. Because the angular scope is defined per DOM element. But you can pass data by using the window object returned from the window.open() method
angular.module('originalModule').service('popupService', function(someOtherDataService) {
var popupWindow = window.open('popupWindow.html');
popupWindow.mySharedData = someOtherDataService.importantData;
});
Once your secondary "popup" angular application is initialized, it can then reference the shared data by reading window.mySharedData.
OR
You could reference the rootscope of the newly spawned window and then communicate an event through the scope of the child window from the parent.
Parent:
var newWindowRef = $window.open("", "New Window", "width=1280,height=890,resizable=1");
var newWindowRootScope = newWindowRef.angular.element("#MY_ROOT_APP_ELEMENT_ID").scope();
newWindowRootScope.$broadcast("INTER_WINDOW_DATA_TRANSFER", {someData : "I'm data"});
Child:
$rootScope.$on("INTER_WINDOW_DATA_TRANSFER", function (data, args) {
console.log("DATA RECEIVED: " + args.someData);
});
Upvotes: 3