Skip to content

feat(uiView): Fire the $onInit hook #2570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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