Skip to content

Commit 11a9111

Browse files
committed
core(controller.has): Add a public "has" method to the Controller class, to check the existence of a given controller. Fixes angular#13951
1 parent 85ef70f commit 11a9111

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/ng/controller.js

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ function $ControllerProvider() {
2727
var controllers = {},
2828
globals = false;
2929

30+
/**
31+
* @ngdoc method
32+
* @name $controllerProvider#has
33+
* @param {string} name Controller name to check.
34+
*/
35+
this.has = function(name) {
36+
return controllers.hasOwnProperty(name);
37+
};
38+
3039
/**
3140
* @ngdoc method
3241
* @name $controllerProvider#register

test/ng/controllerSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ describe('$controller', function() {
5757
expect(ctrl instanceof BarCtrl).toBe(true);
5858
});
5959

60+
it('should return true when having an existing controller, should return false otherwise', function() {
61+
var FooCtrl = function($scope) { $scope.foo = 'foo'; },
62+
BarCtrl = function($scope) { $scope.bar = 'bar'; };
63+
64+
$controllerProvider.register('FooCtrl', noop);
65+
$controllerProvider.register('BarCtrl', ['dep1', 'dep2', noop]);
66+
$controllerProvider.register({
67+
'BazCtrl': noop,
68+
'QuxCtrl': ['dep1', 'dep2', noop]
69+
});
70+
71+
expect($controllerProvider.has('FooCtrl')).toBe(true);
72+
expect($controllerProvider.has('BarCtrl')).toBe(true);
73+
expect($controllerProvider.has('BazCtrl')).toBe(true);
74+
expect($controllerProvider.has('QuxCtrl')).toBe(true);
75+
76+
expect($controllerProvider.has('UnknownCtrl')).toBe(false);
77+
});
6078

6179
it('should allow registration of controllers annotated with arrays', function() {
6280
var FooCtrl = function($scope) { $scope.foo = 'bar'; },

0 commit comments

Comments
 (0)