Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 97fd37e

Browse files
committed
feat(modal): support $uibModalInstance
- Add support for `$uibModalInstance` as a controller local - Deprecate `$modalInstance` support Closes #4638 Closes #4661
1 parent 7c3c631 commit 97fd37e

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

src/modal/docs/demo.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope
3434
// Please note that $modalInstance represents a modal window (instance) dependency.
3535
// It is not the same as the $uibModal service used above.
3636

37-
angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {
37+
angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items) {
3838

3939
$scope.items = items;
4040
$scope.selected = {
4141
item: $scope.items[0]
4242
};
4343

4444
$scope.ok = function () {
45-
$modalInstance.close($scope.selected.item);
45+
$uibModalInstance.close($scope.selected.item);
4646
};
4747

4848
$scope.cancel = function () {
49-
$modalInstance.dismiss('cancel');
49+
$uibModalInstance.dismiss('cancel');
5050
};
5151
});

src/modal/docs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The `$uibModal` service has only one method: `open(options)` where available opt
66
* `templateUrl` - a path to a template representing modal's content
77
* `template` - inline template representing the modal's content
88
* `scope` - a scope instance to be used for the modal's content (actually the `$uibModal` service is going to create a child scope of a provided scope). Defaults to `$rootScope`
9-
* `controller` - a controller for a modal instance - it can initialize scope used by modal. Accepts the "controller-as" syntax in the form 'SomeCtrl as myctrl'; can be injected with `$modalInstance`
9+
* `controller` - a controller for a modal instance - it can initialize scope used by modal. Accepts the "controller-as" syntax in the form 'SomeCtrl as myctrl'; can be injected with `$uibModalInstance`
1010
* `controllerAs` - an alternative to the controller-as syntax, matching the API of directive definitions. Requires the `controller` option to be provided as well
1111
* `bindToController` - when used with `controllerAs` & set to `true`, it will bind the $scope properties onto the controller directly
1212
* `resolve` - members that will be resolved and passed to the controller as locals; it is equivalent of the `resolve` property for AngularJS routes

src/modal/modal.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
554554
backdrop: true, //can also be false or 'static'
555555
keyboard: true
556556
},
557-
$get: ['$injector', '$rootScope', '$q', '$templateRequest', '$controller', '$uibModalStack',
558-
function ($injector, $rootScope, $q, $templateRequest, $controller, $modalStack) {
557+
$get: ['$injector', '$rootScope', '$q', '$templateRequest', '$controller', '$uibModalStack', '$modalSuppressWarning', '$log',
558+
function ($injector, $rootScope, $q, $templateRequest, $controller, $modalStack, $modalSuppressWarning, $log) {
559559
var $modal = {};
560560

561561
function getTemplatePromise(options) {
@@ -641,7 +641,16 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
641641
//controllers
642642
if (modalOptions.controller) {
643643
ctrlLocals.$scope = modalScope;
644-
ctrlLocals.$modalInstance = modalInstance;
644+
ctrlLocals.$uibModalInstance = modalInstance;
645+
Object.defineProperty(ctrlLocals, '$modalInstance', {
646+
get: function() {
647+
if (!$modalSuppressWarning) {
648+
$log.warn('$modalInstance is now deprecated. Use $uibModalInstance instead.');
649+
}
650+
651+
return modalInstance;
652+
}
653+
});
645654
angular.forEach(modalOptions.resolve, function(value, key) {
646655
ctrlLocals[key] = tplAndVars[resolveIter++];
647656
});

src/modal/test/modal.spec.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -531,47 +531,47 @@ describe('$uibModal', function () {
531531

532532
describe('controller', function() {
533533
it('should accept controllers and inject modal instances', function() {
534-
var TestCtrl = function($scope, $modalInstance) {
534+
var TestCtrl = function($scope, $uibModalInstance) {
535535
$scope.fromCtrl = 'Content from ctrl';
536-
$scope.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
536+
$scope.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close);
537537
};
538538

539539
open({template: '<div>{{fromCtrl}} {{isModalInstance}}</div>', controller: TestCtrl});
540540
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
541541
});
542542

543543
it('should accept controllerAs alias', function() {
544-
$controllerProvider.register('TestCtrl', function($modalInstance) {
544+
$controllerProvider.register('TestCtrl', function($uibModalInstance) {
545545
this.fromCtrl = 'Content from ctrl';
546-
this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
546+
this.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close);
547547
});
548548

549549
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: 'TestCtrl as test'});
550550
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
551551
});
552552

553553
it('should respect the controllerAs property as an alternative for the controller-as syntax', function() {
554-
$controllerProvider.register('TestCtrl', function($modalInstance) {
554+
$controllerProvider.register('TestCtrl', function($uibModalInstance) {
555555
this.fromCtrl = 'Content from ctrl';
556-
this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
556+
this.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close);
557557
});
558558

559559
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: 'TestCtrl', controllerAs: 'test'});
560560
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
561561
});
562562

563563
it('should allow defining in-place controller-as controllers', function() {
564-
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: function($modalInstance) {
564+
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: function($uibModalInstance) {
565565
this.fromCtrl = 'Content from ctrl';
566-
this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
566+
this.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close);
567567
}, controllerAs: 'test'});
568568
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
569569
});
570570

571571
it('should allow usage of bindToController', function() {
572-
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: function($modalInstance) {
572+
open({template: '<div>{{test.fromCtrl}} {{test.isModalInstance}}</div>', controller: function($uibModalInstance) {
573573
this.fromCtrl = 'Content from ctrl';
574-
this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close);
574+
this.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close);
575575
}, controllerAs: 'test', bindToController: true});
576576
expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div');
577577
});
@@ -1201,7 +1201,7 @@ describe('$modal deprecation', function() {
12011201
inject(function($modal, $timeout, $log, $rootScope) {
12021202
spyOn($log, 'warn');
12031203

1204-
$modal.open({template: '<div>Foo</div>'});
1204+
$modal.open({template: '<div>Foo</div>', controller: function($modalInstance) {}});
12051205
$rootScope.$digest();
12061206
$timeout.flush(0);
12071207
expect($log.warn.calls.count()).toBe(0);
@@ -1229,16 +1229,17 @@ describe('$modal deprecation', function() {
12291229
'</div>';
12301230
$templateCache.put('template/modal/window.html', windowTemplate);
12311231

1232-
$modal.open({template: '<div>Foo</div>'});
1232+
$modal.open({template: '<div>Foo</div>', controller: function($modalInstance) {}});
12331233
$rootScope.$digest();
12341234
$timeout.flush(0);
12351235

1236-
expect($log.warn.calls.count()).toBe(5);
1236+
expect($log.warn.calls.count()).toBe(6);
12371237
expect($log.warn.calls.argsFor(0)).toEqual(['$modal is now deprecated. Use $uibModal instead.']);
1238-
expect($log.warn.calls.argsFor(1)).toEqual(['$modalStack is now deprecated. Use $uibModalStack instead.']);
1239-
expect($log.warn.calls.argsFor(2)).toEqual(['modal-animation-class is now deprecated. Use uib-modal-animation-class instead.']);
1238+
expect($log.warn.calls.argsFor(1)).toEqual(['$modalInstance is now deprecated. Use $uibModalInstance instead.']);
1239+
expect($log.warn.calls.argsFor(2)).toEqual(['$modalStack is now deprecated. Use $uibModalStack instead.']);
12401240
expect($log.warn.calls.argsFor(3)).toEqual(['modal-animation-class is now deprecated. Use uib-modal-animation-class instead.']);
1241-
expect($log.warn.calls.argsFor(4)).toEqual(['modal-transclude is now deprecated. Use uib-modal-transclude instead.']);
1241+
expect($log.warn.calls.argsFor(4)).toEqual(['modal-animation-class is now deprecated. Use uib-modal-animation-class instead.']);
1242+
expect($log.warn.calls.argsFor(5)).toEqual(['modal-transclude is now deprecated. Use uib-modal-transclude instead.']);
12421243

12431244
$log.warn.calls.reset();
12441245
$compile('<div modal-backdrop></div>')($rootScope);

0 commit comments

Comments
 (0)