diff --git a/src/ng1/viewDirective.ts b/src/ng1/viewDirective.ts index 47bff217f..c22cd78f3 100644 --- a/src/ng1/viewDirective.ts +++ b/src/ng1/viewDirective.ts @@ -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"; @@ -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); } diff --git a/test/viewDirectiveSpec.js b/test/viewDirectiveSpec.js index a31dfecc4..6a827eaf4 100644 --- a/test/viewDirectiveSpec.js +++ b/test/viewDirectiveSpec.js @@ -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']; @@ -40,6 +40,7 @@ describe('uiView', function () { beforeEach(function() { log = ''; + $onInit = jasmine.createSpy('$onInit'); }); var aState = { @@ -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) { @@ -147,6 +155,7 @@ describe('uiView', function () { controller: function($scope) { log += 'ctrl(n);'; } }) .state('o', oState) + .state('p', pState) })); beforeEach(inject(function ($rootScope, _$compile_) { @@ -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('
')(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) {