AngulaJS using $rootScope in config phase for ngDialogProvider, or using ngDialogProvider in run phase

428 views Asked by At

I know that you cannot inject $rootScope into the config phase of the application, AND I know that you can inject $rootScope in the run phase, but ngDialogProvider does not inject in the run phase...

I am using ngDialog and it has a ngDialogProvider where I can set the defaults of all my ngDailogs (I have lots)

I need to set a flag on the rootScope to prevent Navigation while the ngDailog is open.

How can I achieve this if we cannot inject rootScope in the config phase?

.config(['$rootScope','ngDialogProvider', function ($rootScope,ngDialogProvider) {
    // Inject Error: $rootScope cannot be injected here
    ngDialogProvider.setDefaults({
        onOpenCallback: function() {
            $rootScope.preventNavigation = true; 
        },
        preCloseCallback: function() {
            $rootScope.preventNavigation = false;
        }
    });
}])
1

There are 1 answers

9
Ved On BEST ANSWER

You should do this in .run

.run(['$rootScope','ngDialogProvider', function ($rootScope,ngDialogProvider) {
    // Inject Error: $rootScope cannot be injected here
    ngDialogProvider.setDefaults({
        onOpenCallback: function() {
            $rootScope.preventNavigation = true; 
        },
        preCloseCallback: function() {
            $rootScope.preventNavigation = false;
        }
    });
}])
ngDialogProvider is available in .config phase. Need to check if it is available on .run phase or not.

Edit 1: You can use global variable:

app.constant(‘preventNavigation’,false or true);

.config(['ngDialogProvider','preCloseCallback', function (ngDialogProvider,preCloseCallback) {
    ngDialogProvider.setDefaults({
        onOpenCallback: function() {
            preventNavigation = true; 
        },
        preCloseCallback: function() {
            preventNavigation = false;
        }
    });
}])

constant should never be changed (though it is still possible to change it programmatically in Angular 1.x).