mgcrea / angular-strap

AngularJS 1.2+ native directives for Bootstrap 3.
mgcrea.github.io/angular-strap
MIT License
5.73k stars 1.38k forks source link

Angular 1.5 $onInit and $onDestroy lifecycle hooks are not triggered for Modal dialogs #2213

Closed karan-kang closed 5 years ago

karan-kang commented 7 years ago

Modal dialog controllers currently do not trigger $onInit and $onDestroy life cycles hooks.

DaleWebb commented 7 years ago

Are these life cycle hooks part of Angular 1.5? Or does it exist in 1.4 too?

karan-kang commented 7 years ago

These life cycle hooks are part of Angular 1.5. You can read more about them here: https://docs.angularjs.org/api/ng/service/$compile#life-cycle-hooks or https://blog.thoughtram.io/angularjs/2016/03/29/exploring-angular-1.5-lifecycle-hooks.html

karan-kang commented 7 years ago

For now, I am using this a workaround. This only works if the modal controller is using the controllerAs syntax:

$modal({
    ...,
    onBeforeShow: function($modal) {
        // Angular 1.5 $onInit and $onDestroy life cycle event support
        if ($modal.$options.controllerAs) {
            var modalPromise = $modal.$promise;
            if (modalPromise) {
                modalPromise.then(function(val) {
                    if (val && val.locals) {
                        var modalScope = val.locals.$scope;
                        if (modalScope) {
                            var controller = modalScope[$modal.$options.controllerAs];
                            if (controller) {
                                // Manually trigger $onInit callback
                                if (angular.isFunction(controller.$onInit)) {
                                    controller.$onInit();
                                }
                                // Manually trigger $onDestroy callback on scope destroy
                                if (angular.isFunction(controller.$onDestroy)) {
                                    modalScope.$on('$destroy', controller.$onDestroy);
                                }
                            }
                        }
                    }
                });
            }
        }
    }
})

This workaround should work with $compileProvider.debugInfoEnabled(false);

DaleWebb commented 7 years ago

Are you able to figure out where the changes could be natively for Angular-Strap?

If so, a PR would be welcome 👍

cstuncsik commented 7 years ago

You can decorate the $modal service generally not just the actual instance (based on @Karankang007's solution)

https://gist.github.com/cstuncsik/4b6da0b2df0e02e1c842c28ddace344a

neekersMF commented 6 years ago

Is there an example of how to call the $modal with a component?

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

lock[bot] commented 5 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.