From 2ab95b131ba8c80244d43c314ce086cd0cfb5996 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Fri, 19 Feb 2016 00:18:26 -0500 Subject: [PATCH] feat(modal): Call $onInit on controller if defined --- src/modal/modal.js | 3 +++ src/modal/test/modal.spec.js | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/modal/modal.js b/src/modal/modal.js index 3025e0d136..f591088161 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -687,6 +687,9 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap']) ctrlInstance.$close = modalScope.$close; ctrlInstance.$dismiss = modalScope.$dismiss; angular.extend(ctrlInstance, providedScope); + if (angular.isFunction(ctrlInstance.$onInit)) { + ctrlInstance.$onInit(); + } } modalScope[modalOptions.controllerAs] = ctrlInstance; diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index fae3a34556..84d22f50ba 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -826,6 +826,27 @@ describe('$uibModal', function() { }); expect($document).toHaveModalOpenWithContent('Content from ctrl true bar', 'div'); }); + + it('should have $onInit called', function() { + var $scope = $rootScope.$new(true); + var $onInit = jasmine.createSpy('$onInit'); + $scope.foo = 'bar'; + open({ + template: '
{{test.fromCtrl}} {{test.closeDismissPresent()}} {{test.foo}}
', + controller: function($uibModalInstance) { + this.$onInit = $onInit; + this.fromCtrl = 'Content from ctrl'; + this.closeDismissPresent = function() { + return angular.isFunction(this.$close) && angular.isFunction(this.$dismiss); + }; + }, + controllerAs: 'test', + bindToController: true, + scope: $scope + }); + expect($document).toHaveModalOpenWithContent('Content from ctrl true bar', 'div'); + expect($onInit).toHaveBeenCalled(); + }); }); describe('resolve', function() {