2

I want to ask if the parent scope destroy, does the child scope will also be destroy?

I ask this question is because i'm using the ngdialog to create some modal dialogs. There are 2 dialogs, A -> B, we can open dialog A from a web page, and dialog A is parent of dialog B, click button on A to open B.

I want to open dialog B and close A(the parent of B) at the same time, and the data in scope B are all missing. So i think maybe it is caused by this. can anyone help me to get better understanding on this?

dialogACtrl:

$scope.selectServer = function (serverType) {
        $scope.serverType = serverType;
        $scope.closeThisDialog('close');    //close current dialog
        var newScope = $scope.$new(true);

        var modalInstance;

        modalInstance = ngDialog.openConfirm({
            template: 'servers/templates/dialog-b.tpl.html',
            scope: newScope,
            controller: 'dialogBCtrl',
            resolve: {
                serverData: function () {
                    return null;
                },
                delegate: callbackWrapper
            }
        });
        siteScopeModalInstance.result.then(callback);

ngDialog source:

open: function (opts) {
    ...
    scopes[dialogID] = scope = angular.isObject(options.scope) ? options.scope.$new() : $rootScope.$new();



performCloseDialog: function ($dialog, value) {
    ...
    scope.$destroy();
huan feng
  • 7,307
  • 2
  • 32
  • 56
  • How do 2 dialogs have parent/child relationship? Please show relevant code. This sounds more like a misunderstanding of controller instances – charlietfl Nov 09 '15 at 13:07

2 Answers2

1

When a parent scope is destroyed, it basically cascade destroy it's child scopes tree.

Pierre Gayvallet
  • 2,933
  • 2
  • 23
  • 37
1

I suspect this has nothing to do with parent/child scopes

Each instance of the dialog will initialize it's own controller instance. This means that when you open the second dialog, it has it's own scope that is different than the scope in the first dialog.

Any data you need to pass from one to the other would need to be either done with a resolve or by using a service to share data across controllers.

Without any code posted in your question it is hard to help further

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • I read the source of ngDialog, it will use the scope passed in as parent scope. If we don't pass in the scope, it will use rootScope as parent to create a new scope. So i think it is caused by the parent/child scopes, please see the code i just updated. – huan feng Nov 10 '15 at 05:33
  • @huanfeng still not enough shown to understand what `data in scope B are all missing` means. Use a service to share data – charlietfl Nov 10 '15 at 11:02