Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit eff13aa

Browse files
committed
docs($compile): create new error for "missing controller identifier"
1 parent a7b2e52 commit eff13aa

File tree

4 files changed

+99
-76
lines changed

4 files changed

+99
-76
lines changed

docs/content/error/$compile/noctrl.ngdoc

+5-61
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,9 @@
44
@description
55

66
When using the `bindToController` feature of AngularJS, a directive is required
7-
to have a Controller, in addition to a controller identifier.
7+
to have a Controller. A controller may be specified by adding a "controller"
8+
property to the directive definition object. Its value should be either a
9+
string, or an invokable object (a function, or an array whose last element is a
10+
function).
811

9-
For example, the following directives are valid:
10-
11-
```js
12-
// OKAY, because controller is a string with a label component.
13-
directive("okay", function() {
14-
return {
15-
bindToController: true,
16-
controller: "myCtrl as $ctrl"
17-
scope: {
18-
text: "@text"
19-
}
20-
};
21-
});
22-
23-
24-
// OKAY, because the directive uses the controllerAs property to override
25-
// the controller identifier.
26-
directive("okay2", function() {
27-
return {
28-
bindToController: true,
29-
controllerAs: "$ctrl",
30-
controller: function() {
31-
32-
},
33-
scope: {
34-
text: "@text"
35-
}
36-
};
37-
});
38-
```
39-
40-
While the following are invalid:
41-
42-
```js
43-
// BAD, because the controller property is a string with no identifier.
44-
directive("bad", function() {
45-
return {
46-
bindToController: true,
47-
controller: "unlabeledCtrl",
48-
scope: {
49-
text: "@text"
50-
}
51-
};
52-
});
53-
54-
55-
// BAD because the controller is not a string (therefore has no identifier),
56-
// and there is no controllerAs property.
57-
directive("bad2", function() {
58-
return {
59-
bindToController: true,
60-
controller: function noControllerAs() {
61-
62-
},
63-
scope: {
64-
text: "@text"
65-
}
66-
};
67-
});
68-
```
12+
For more information, see the {@link guide.directive directives guide}.
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
@ngdoc error
2+
@name $compile:noident
3+
@fullName Controller alias is required.
4+
@description
5+
6+
When using the `bindToController` feature of AngularJS, a directive is required
7+
to have a Controller identifier. This can be supplied using the "controllerAs"
8+
property of the directive object, or alternatively by adding " as ALIAS" to the
9+
controller name (in either the "controller" property, or where the controller
10+
was registered).
11+
12+
For example, the following directives are valid:
13+
14+
```js
15+
// OKAY, because controller is a string with a label component.
16+
directive("okay", function() {
17+
return {
18+
bindToController: true,
19+
controller: "myCtrl as $ctrl"
20+
scope: {
21+
text: "@text"
22+
}
23+
};
24+
});
25+
26+
27+
// OKAY, because the directive uses the controllerAs property to override
28+
// the controller identifier.
29+
directive("okay2", function() {
30+
return {
31+
bindToController: true,
32+
controllerAs: "$ctrl",
33+
controller: function() {
34+
35+
},
36+
scope: {
37+
text: "@text"
38+
}
39+
};
40+
});
41+
```
42+
43+
While the following are invalid:
44+
45+
```js
46+
// BAD, because the controller property is a string with no identifier.
47+
directive("bad", function() {
48+
return {
49+
bindToController: true,
50+
controller: "unlabeledCtrl",
51+
scope: {
52+
text: "@text"
53+
}
54+
};
55+
});
56+
57+
58+
// BAD because the controller is not a string (therefore has no identifier),
59+
// and there is no controllerAs property.
60+
directive("bad2", function() {
61+
return {
62+
bindToController: true,
63+
controller: function noControllerAs() {
64+
65+
},
66+
scope: {
67+
text: "@text"
68+
}
69+
};
70+
});
71+
```

src/ng/compile.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,17 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
763763
var controller = directive.controller;
764764
var controllerAs = directive.controllerAs;
765765
if (!directive.controller || !identifierForController(controller, controllerAs)) {
766-
throw $compileMinErr('noctrl',
767-
"Cannot bind to controller without directive '{0}'s controller.",
768-
directiveName);
766+
if (directive.controller) {
767+
// There is a controller, but no identifier or controllerAs property
768+
throw $compileMinErr('noident',
769+
"Cannot bind to controller without controller alias for directive '{0}'.",
770+
directiveName);
771+
} else {
772+
// There is no controller, there may or may not be a controllerAs property
773+
throw $compileMinErr('noctrl',
774+
"Cannot bind to controller without directive '{0}'s controller.",
775+
directiveName);
776+
}
769777
}
770778
}
771779
return bindings;

test/ng/compileSpec.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -3833,9 +3833,9 @@ describe('$compile', function() {
38333833
});
38343834

38353835

3836-
it('should throw noctrl when missing controllerAs label', function() {
3836+
it('should throw noident when missing controllerAs label', function() {
38373837
module(function($compileProvider) {
3838-
$compileProvider.directive('noCtrl', valueFn({
3838+
$compileProvider.directive('noAlias', valueFn({
38393839
templateUrl: 'test.html',
38403840
scope: {
38413841
'data': '=dirData',
@@ -3848,17 +3848,17 @@ describe('$compile', function() {
38483848
});
38493849
inject(function($compile, $rootScope) {
38503850
expect(function() {
3851-
$compile('<div no-ctrl>')($rootScope);
3852-
}).toThrowMinErr('$compile', 'noctrl',
3853-
'Cannot bind to controller without directive \'noCtrl\'s controller.');
3851+
$compile('<div no-alias>')($rootScope);
3852+
}).toThrowMinErr('$compile', 'noident',
3853+
'Cannot bind to controller without controller alias for directive \'noAlias\'.');
38543854
});
38553855
});
38563856

38573857

3858-
it('should throw noctrl when missing controller label', function() {
3858+
it('should throw noident when missing controller identifier', function() {
38593859
module(function($compileProvider, $controllerProvider) {
38603860
$controllerProvider.register('myCtrl', function() {});
3861-
$compileProvider.directive('noCtrl', valueFn({
3861+
$compileProvider.directive('noAlias', valueFn({
38623862
templateUrl: 'test.html',
38633863
scope: {
38643864
'data': '=dirData',
@@ -3871,9 +3871,9 @@ describe('$compile', function() {
38713871
});
38723872
inject(function($compile, $rootScope) {
38733873
expect(function() {
3874-
$compile('<div no-ctrl>')($rootScope);
3875-
}).toThrowMinErr('$compile', 'noctrl',
3876-
'Cannot bind to controller without directive \'noCtrl\'s controller.');
3874+
$compile('<div no-alias>')($rootScope);
3875+
}).toThrowMinErr('$compile', 'noident',
3876+
'Cannot bind to controller without controller alias for directive \'noAlias\'.');
38773877
});
38783878
});
38793879

@@ -3998,7 +3998,7 @@ describe('$compile', function() {
39983998
});
39993999
expect(this.str).toBe('Hello, world!');
40004000
expect(this.fn()).toBe('called!');
4001-
}
4001+
};
40024002

40034003
module(function($compileProvider, $controllerProvider) {
40044004
$controllerProvider.register('myCtrl', function() {
@@ -4052,7 +4052,7 @@ describe('$compile', function() {
40524052
});
40534053
expect(this.str).toBe('Hello, world!');
40544054
expect(this.fn()).toBe('called!');
4055-
}
4055+
};
40564056

40574057
module(function($compileProvider, $controllerProvider) {
40584058
$controllerProvider.register('myCtrl', function() {

0 commit comments

Comments
 (0)