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

Commit 9e6db1a

Browse files
feat($compile): allow required controllers to be bound to the directive controller
If directives are required through an object hash, rather than a string or array, the required directives' controllers are bound to the current directive's controller in much the same way as the properties are bound to using `bindToController`. The binding is done after the controller has been constructed and all the bindings are guaranteed to be complete by the time the controller's `$onInit` method is called. This change makes it much simpler to access require controllers without the need for manually wiring them up in link functions. In particular this enables support for `require` in directives defined using `mod.component()`
1 parent 8040bab commit 9e6db1a

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/ng/compile.js

+78
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@
279279
* passed to the linking function will also be an object with matching keys, whose values will hold the corresponding
280280
* controllers.
281281
*
282+
* If the `require` property is an object and the directive provides a controller, then the required controllers are
283+
* bound to the controller using the keys of the `require` property. See the {@link $compileProvider#component} helper
284+
* for an example of how this can be used.
285+
*
282286
* If no such directive(s) can be found, or if the directive does not have a controller, then an error is raised
283287
* (unless no link function is specified, in which case error checking is skipped). The name can be prefixed with:
284288
*
@@ -1025,6 +1029,80 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
10251029
*
10261030
* ```
10271031
*
1032+
* ### Intercomponent Communication
1033+
* Directives can require the controllers of other directives to enable communication
1034+
* between the directives. This can be achieved in a component by providing an
1035+
* object mapping for the `require` property. Here is the tab pane example built
1036+
* from components...
1037+
*
1038+
* <example module="docsTabsExample">
1039+
* <file name="script.js">
1040+
* angular.module('docsTabsExample', [])
1041+
* .component('myTabs', {
1042+
* transclude: true,
1043+
* controller: function() {
1044+
* var panes = this.panes = [];
1045+
*
1046+
* this.select = function(pane) {
1047+
* angular.forEach(panes, function(pane) {
1048+
* pane.selected = false;
1049+
* });
1050+
* pane.selected = true;
1051+
* };
1052+
*
1053+
* this.addPane = function(pane) {
1054+
* if (panes.length === 0) {
1055+
* this.select(pane);
1056+
* }
1057+
* panes.push(pane);
1058+
* };
1059+
* },
1060+
* templateUrl: 'my-tabs.html'
1061+
* })
1062+
* .component('myPane', {
1063+
* transclude: true,
1064+
* require: {tabsCtrl: '^myTabs'},
1065+
* bindings: {
1066+
* title: '@'
1067+
* },
1068+
* controller: function() {
1069+
* this.$onInit = function() {
1070+
* this.tabsCtrl.addPane(this);
1071+
* console.log(this);
1072+
* };
1073+
* },
1074+
* templateUrl: 'my-pane.html'
1075+
* });
1076+
* </file>
1077+
* <file name="index.html">
1078+
* <my-tabs>
1079+
* <my-pane title="Hello">
1080+
* <h4>Hello</h4>
1081+
* <p>Lorem ipsum dolor sit amet</p>
1082+
* </my-pane>
1083+
* <my-pane title="World">
1084+
* <h4>World</h4>
1085+
* <em>Mauris elementum elementum enim at suscipit.</em>
1086+
* <p><a href ng-click="i = i + 1">counter: {{i || 0}}</a></p>
1087+
* </my-pane>
1088+
* </my-tabs>
1089+
* </file>
1090+
* <file name="my-tabs.html">
1091+
* <div class="tabbable">
1092+
* <ul class="nav nav-tabs">
1093+
* <li ng-repeat="pane in $ctrl.panes" ng-class="{active:pane.selected}">
1094+
* <a href="" ng-click="$ctrl.select(pane)">{{pane.title}}</a>
1095+
* </li>
1096+
* </ul>
1097+
* <div class="tab-content" ng-transclude></div>
1098+
* </div>
1099+
* </file>
1100+
* <file name="my-pane.html">
1101+
* <div class="tab-pane" ng-show="$ctrl.selected" ng-transclude></div>
1102+
* </file>
1103+
* </example>
1104+
*
1105+
*
10281106
* <br />
10291107
* Components are also useful as route templates (e.g. when using
10301108
* {@link ngRoute ngRoute}):

0 commit comments

Comments
 (0)