Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bcaa151

Browse files
committedFeb 23, 2016
core(controller.has): Add a public "has" method to the Controller class, to check the existence of a given controller. Fixes #13951
1 parent 85ef70f commit bcaa151

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
 

‎src/ng/controller.js

Lines changed: 9 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ 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+
$controllerProvider.register('FooCtrl', noop);
62+
$controllerProvider.register('BarCtrl', ['dep1', 'dep2', noop]);
63+
$controllerProvider.register({
64+
'BazCtrl': noop,
65+
'QuxCtrl': ['dep1', 'dep2', noop]
66+
});
67+
68+
expect($controllerProvider.has('FooCtrl')).toBe(true);
69+
expect($controllerProvider.has('BarCtrl')).toBe(true);
70+
expect($controllerProvider.has('BazCtrl')).toBe(true);
71+
expect($controllerProvider.has('QuxCtrl')).toBe(true);
72+
73+
expect($controllerProvider.has('UnknownCtrl')).toBe(false);
74+
});
6075

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

0 commit comments

Comments
 (0)
This repository has been archived.