Skip to content

Commit b090ca0

Browse files
Daniel Smithchristopherthielen
Daniel Smith
authored andcommitted
feat(uiView): Fire the $onInit hook
After instantiating the controller, will fire its $onInit if defined. Closes #2559
1 parent a928783 commit b090ca0

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/viewDirective.js

+1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ function $ViewDirectiveFill ( $compile, $controller, $state, $interpolate
332332
scope[locals.$$controllerAs] = controller;
333333
scope[locals.$$controllerAs][locals.$$resolveAs] = resolveData;
334334
}
335+
if (isFunction(controller.$onInit)) controller.$onInit();
335336
$element.data('$ngControllerController', controller);
336337
$element.children().data('$ngControllerController', controller);
337338
}

test/viewDirectiveSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,22 @@ describe('uiView', function () {
420420
}));
421421
});
422422

423+
it('should call the existing $onInit after instantiating a controller', inject(function ($state, $q) {
424+
var $onInit = jasmine.createSpy();
425+
$stateProvider.state('onInit', {
426+
controller: function () {
427+
this.$onInit = $onInit;
428+
},
429+
template: "hi",
430+
controllerAs: "vm"
431+
});
432+
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
433+
$state.transitionTo('onInit');
434+
$q.flush();
435+
436+
expect($onInit).toHaveBeenCalled();
437+
}));
438+
423439
describe('play nicely with other directives', function() {
424440
// related to issue #857
425441
it('should work with ngIf', inject(function ($state, $q, $compile) {

0 commit comments

Comments
 (0)