Skip to content

Commit

Permalink
feat($compile): call $ngOnInit on directive controllers after contr…
Browse files Browse the repository at this point in the history
…oller construction

This enables option three of angular#13510 (comment)
by allowing the creator of directive controllers using ES6 classes to have a hook
that is called when the bindings are definitely available.

Moreover this will help solve the problem of accessing `require`d controllers
from controller instances without resorting to wiring up in a `link` function.
See angular#5893
  • Loading branch information
petebacondarwin committed Jan 14, 2016
1 parent 59feecc commit 8040bab
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4936,21 +4936,21 @@ describe('$compile', function() {

it('should call `controller.$onInit`, if provided after all the controllers have been constructed', function() {

function Controller1($element) { this.id = 1; this.element = $element; }
Controller1.prototype.$onInit = jasmine.createSpy('$onInit').andCallFake(function() {
function check() {
/*jshint validthis:true */
expect(this.element.controller('d1').id).toEqual(1);
expect(this.element.controller('d2').id).toEqual(2);
});
}

function Controller1($element) { this.id = 1; this.element = $element; }
Controller1.prototype.$onInit = jasmine.createSpy('$onInit').andCallFake(check);

function Controller2($element) { this.id = 2; this.element = $element; }
Controller2.prototype.$onInit = jasmine.createSpy('$onInit').andCallFake(function() {
expect(this.element.controller('d1').id).toEqual(1);
expect(this.element.controller('d2').id).toEqual(2);
});
Controller2.prototype.$onInit = jasmine.createSpy('$onInit').andCallFake(check);

angular.module('my', [])
.directive('d1', function() { return { controller: Controller1 }; })
.directive('d2', function() { return { controller: Controller2 }; });
.directive('d1', valueFn({ controller: Controller1 }))
.directive('d2', valueFn({ controller: Controller2 }));

module('my');
inject(function($compile, $rootScope) {
Expand Down

0 comments on commit 8040bab

Please sign in to comment.