Skip to content

Commit c8afc38

Browse files
author
Daniel Smith
committed
feat(uiView): Fire the $onInit hook
After instantiating the controller, will fire its $onInit if defined. Closes #2559
1 parent 624d94e commit c8afc38

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/ng1/viewDirective.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var ngMajorVer = angular.version.major;
22
var ngMinorVer = angular.version.minor;
33
/** @module view */ /** for typedoc */
44
import {extend} from "../common/common";
5-
import {isDefined} from "../common/predicates";
5+
import {isDefined, isFunction} from "../common/predicates";
66
import {trace} from "../common/trace";
77
import {ViewConfig} from "../view/view";
88
import {UIViewData} from "../view/interface";
@@ -319,6 +319,7 @@ function $ViewDirectiveFill ( $compile, $controller, $interpolate, $injec
319319
let locals = data.$locals;
320320
let controllerInstance = $controller(controller, extend(locals, { $scope: scope, $element: $element }));
321321
if (controllerAs) scope[controllerAs] = controllerInstance;
322+
if (isFunction(controllerInstance.$onInit)) controllerInstance.$onInit();
322323
$element.data('$ngControllerController', controllerInstance);
323324
$element.children().data('$ngControllerController', controllerInstance);
324325
}

test/viewDirectiveSpec.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function animateFlush($animate) {
1414
describe('uiView', function () {
1515
'use strict';
1616

17-
var log, scope, $compile, elem;
17+
var log, scope, $compile, elem, $onInit;
1818

1919
beforeEach(function() {
2020
var depends = ['ui.router', 'ui.router.state.events'];
@@ -40,6 +40,7 @@ describe('uiView', function () {
4040

4141
beforeEach(function() {
4242
log = '';
43+
$onInit = jasmine.createSpy('$onInit');
4344
});
4445

4546
var aState = {
@@ -119,6 +120,13 @@ describe('uiView', function () {
119120
controller: function ($scope, $element) {
120121
$scope.elementId = $element.attr('id');
121122
}
123+
},
124+
pState = {
125+
controller: function() {
126+
this.$onInit = $onInit;
127+
},
128+
template: "hi",
129+
controllerAs: "vm"
122130
};
123131

124132
beforeEach(module(function ($stateProvider) {
@@ -147,6 +155,7 @@ describe('uiView', function () {
147155
controller: function($scope) { log += 'ctrl(n);'; }
148156
})
149157
.state('o', oState)
158+
.state('p', pState)
150159
}));
151160

152161
beforeEach(inject(function ($rootScope, _$compile_) {
@@ -349,6 +358,14 @@ describe('uiView', function () {
349358
expect(elem.text()).toBe('oState');
350359
}));
351360

361+
it('should call the existing $onInit after instantiating a controller', inject(function ($state, $q) {
362+
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
363+
$state.transitionTo(pState);
364+
$q.flush();
365+
366+
expect($onInit).toHaveBeenCalled();
367+
}));
368+
352369
describe('play nicely with other directives', function() {
353370
// related to issue #857
354371
it('should work with ngIf', inject(function ($state, $q, $compile) {

0 commit comments

Comments
 (0)