8000 `$onInit` and friends by petebacondarwin · Pull Request #13763 · angular/angular.js · GitHub
[go: up one dir, main page]

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

$onInit and friends #13763

Closed
wants to merge 20 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
50b0c1a
feat($compile): call `$ngOnInit` on directive controllers after contr…
petebacondarwin Jan 13, 2016
cf4e7be
test($compile): check that $onInit is called correctly for ES6 classes
petebacondarwin Jan 13, 2016
452dd39
docs($compile): document the new `$onInit` controller hook
petebacondarwin Jan 13, 2016
f72ecbd
feat($compile): allow `require` to be an object
petebacondarwin Jan 13, 2016
b2c0b05
feat($compile): allow required controllers to be bound to the directi…
petebacondarwin Jan 13, 2016
37793ce
feat($compile): allow `require` to be an object
petebacondarwin Jan 14, 2016
a12ddc6
test($compile): check explicit return controllers are not broken by b…
petebacondarwin Jan 14, 2016
59feecc
feat($compile): allow required controllers to be bound to the directi…
petebacondarwin Jan 14, 2016
8040bab
feat($compile): call `$ngOnInit` on directive controllers after contr…
petebacondarwin Jan 14, 2016
9e6db1a
feat($compile): allow required controllers to be bound to the directi…
petebacondarwin Jan 14, 2016
1d18df2
fix($compile): ensure controllers with return value constructors are …
petebacondarwin Jan 15, 2016
4b304a6
docs($compile): squash me
petebacondarwin Jan 15, 2016
cae40ca
fix($compile): only bind required controllers if `bindToController` i…
petebacondarwin Jan 15, 2016
276b9ee
docs($compile): fix typo
petebacondarwin Jan 15, 2016
6d66a75
fix($compile): only bind required controllers if `bindToController` i…
petebacondarwin Jan 15, 2016
32b7da3
test($compile): check that $onInit is called correctly for ES6 classes
petebacondarwin Jan 15, 2016
cb495a5
fix($compile): only bind required controllers if `bindToController` i…
petebacondarwin Jan 15, 2016
270e230
fix($compile): ensure controllers with return value constructors are …
petebacondarwin Jan 15, 2016
281d987
fix($compile): only bind required controllers if `bindToController` i…
petebacondarwin Jan 15, 2016
9f76a11
test($compile): check that $onInit is called correctly for ES6 classes
petebacondarwin Jan 15, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat($compile): allow required controllers to be bound to the directi…
…ve 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()`
  • Loading branch information
petebacondarwin committed Jan 14, 2016
commit 9e6db1a267f505c793c49c73631a602ddcf6e98b
78 changes: 78 additions & 0 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@
* passed to the linking function will also be an object with matching keys, whose values will hold the corresponding
* controllers.
*
* If the `require` property is an object and the directive provides a controller, then the required controllers are
* bound to the controller using the keys of the `require` property. See the {@link $compileProvider#component} helper
* for an example of how this can be used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should note that the controllers are only available once $onInit has been called, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

*
* If no such directive(s) can be found, or if the directive does not have a controller, then an error is raised
* (unless no link function is specified, in which case error checking is skipped). The name can be prefixed with:
*
Expand Down Expand Up @@ -1025,6 +1029,80 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
*
* ```
*
* ### Intercomponent Communication
* Directives can require the controllers of other directives to enable communication
* between the directives. This can be achieved in a component by providing an
* object mapping for the `require` property. Here is the tab pane example built
* from components...
*
* <example module="docsTabsExample">
* <file name="script.js">
* angular.module('docsTabsExample', [])
* .component('myTabs', {
* transclude: true,
* controller: function() {
* var panes = this.panes = [];
*
* this.select = function(pane) {
* angular.forEach(panes, function(pane) {
* pane.selected = false;
* });
* pane.selected = true;
* };
*
* this.addPane = function(pane) {
* if (panes.length === 0) {
* this.select(pane);
* }
* panes.push(pane);
* };
* },
* templateUrl: 'my-tabs.html'
* })
* .component('myPane', {
* transclude: true,
* require: {tabsCtrl: '^myTabs'},
* bindings: {
* title: '@'
* },
* controller: function() {
* this.$onInit = function() {
* this.tabsCtrl.addPane(this);
* console.log(this);
* };
* },
* templateUrl: 'my-pane.html'
* });
* </file>
* <file name="index.html">
* <my-tabs>
* <my-pane title="Hello">
* <h4>Hello</h4>
* <p>Lorem ipsum dolor sit amet</p>
* </my-pane>
* <my-pane title="World">
* <h4>World</h4>
* <em>Mauris elementum elementum enim at suscipit.</em>
* <p><a href ng-click="i = i + 1">counter: {{i || 0}}</a></p>
* </my-pane>
* </my-tabs>
* </file>
* <file name="my-tabs.html">
* <div class="tabbable">
* <ul class="nav nav-tabs">
* <li ng-repeat="pane in $ctrl.panes" ng-class="{active:pane.selected}">
* <a href="" ng-click="$ctrl.select(pane)">{{pane.title}}</a>
* </li>
* </ul>
* <div class="tab-content" ng-transclude></div>
* </div>
* </file>
* <file name="my-pane.html">
* <div class="tab-pane" ng-show="$ctrl.selected" ng-transclude></div>
* </file>
* </example>
*
*
* <br />
* Components are also useful as route templates (e.g. when using
* {@link ngRoute ngRoute}):
Expand Down
0