|
| 1 | +@ngdoc error |
| 2 | +@name $compile:noctrl |
| 3 | +@fullName Controller is required. |
| 4 | +@description |
| 5 | + |
| 6 | +When using the `bindToController` feature of AngularJS, a directive is required |
| 7 | +to have a Controller, in addition to a controller identifier. |
| 8 | + |
| 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 | +``` |
0 commit comments