Skip to content

Commit

Permalink
feat(uiView): Fire the $onInit hook
Browse files Browse the repository at this point in the history
After instantiating the controller, will fire its $onInit if defined.

Closes #2559
  • Loading branch information
Daniel Smith committed Feb 19, 2016
1 parent 624d94e commit c8afc38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/ng1/viewDirective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var ngMajorVer = angular.version.major;
var ngMinorVer = angular.version.minor;
/** @module view */ /** for typedoc */
import {extend} from "../common/common";
import {isDefined} from "../common/predicates";
import {isDefined, isFunction} from "../common/predicates";
import {trace} from "../common/trace";
import {ViewConfig} from "../view/view";
import {UIViewData} from "../view/interface";
Expand Down Expand Up @@ -319,6 +319,7 @@ function $ViewDirectiveFill ( $compile, $controller, $interpolate, $injec
let locals = data.$locals;
let controllerInstance = $controller(controller, extend(locals, { $scope: scope, $element: $element }));
if (controllerAs) scope[controllerAs] = controllerInstance;
if (isFunction(controllerInstance.$onInit)) controllerInstance.$onInit();
$element.data('$ngControllerController', controllerInstance);
$element.children().data('$ngControllerController', controllerInstance);
}
Expand Down
19 changes: 18 additions & 1 deletion test/viewDirectiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function animateFlush($animate) {
describe('uiView', function () {
'use strict';

var log, scope, $compile, elem;
var log, scope, $compile, elem, $onInit;

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

beforeEach(function() {
log = '';
$onInit = jasmine.createSpy('$onInit');
});

var aState = {
Expand Down Expand Up @@ -119,6 +120,13 @@ describe('uiView', function () {
controller: function ($scope, $element) {
$scope.elementId = $element.attr('id');
}
},
pState = {
controller: function() {
this.$onInit = $onInit;
},
template: "hi",
controllerAs: "vm"
};

beforeEach(module(function ($stateProvider) {
Expand Down Expand Up @@ -147,6 +155,7 @@ describe('uiView', function () {
controller: function($scope) { log += 'ctrl(n);'; }
})
.state('o', oState)
.state('p', pState)
}));

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

it('should call the existing $onInit after instantiating a controller', inject(function ($state, $q) {
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
$state.transitionTo(pState);
$q.flush();

expect($onInit).toHaveBeenCalled();
}));

describe('play nicely with other directives', function() {
// related to issue #857
it('should work with ngIf', inject(function ($state, $q, $compile) {
Expand Down

0 comments on commit c8afc38

Please sign in to comment.