Skip to content

Commit b2860fe

Browse files
petebacondarwinNarretz
authored andcommitted
feat($compile): call $ngOnInit on directive controllers after controller 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
1 parent e78b376 commit b2860fe

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

test/ng/compileSpec.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -4936,21 +4936,21 @@ describe('$compile', function() {
49364936

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

4939-
function Controller1($element) { this.id = 1; this.element = $element; }
4940-
Controller1.prototype.$onInit = jasmine.createSpy('$onInit').andCallFake(function() {
4939+
function check() {
4940+
/*jshint validthis:true */
49414941
expect(this.element.controller('d1').id).toEqual(1);
49424942
expect(this.element.controller('d2').id).toEqual(2);
4943-
});
4943+
}
4944+
4945+
function Controller1($element) { this.id = 1; this.element = $element; }
4946+
Controller1.prototype.$onInit = jasmine.createSpy('$onInit').andCallFake(check);
49444947

49454948
function Controller2($element) { this.id = 2; this.element = $element; }
4946-
Controller2.prototype.$onInit = jasmine.createSpy('$onInit').andCallFake(function() {
4947-
expect(this.element.controller('d1').id).toEqual(1);
4948-
expect(this.element.controller('d2').id).toEqual(2);
4949-
});
4949+
Controller2.prototype.$onInit = jasmine.createSpy('$onInit').andCallFake(check);
49504950

49514951
angular.module('my', [])
4952-
.directive('d1', function() { return { controller: Controller1 }; })
4953-
.directive('d2', function() { return { controller: Controller2 }; });
4952+
.directive('d1', valueFn({ controller: Controller1 }))
4953+
.directive('d2', valueFn({ controller: Controller2 }));
49544954

49554955
module('my');
49564956
inject(function($compile, $rootScope) {

0 commit comments

Comments
 (0)