diff --git a/src/router-directive.es5.js b/src/router-directive.es5.js index ad686c5..dfda12b 100644 --- a/src/router-directive.es5.js +++ b/src/router-directive.es5.js @@ -11,7 +11,6 @@ angular.module('ngNewRouter', []) .factory('$$pipeline', privatePipelineFactory) .factory('$setupRoutersStep', setupRoutersStepFactory) .factory('$initLocalsStep', initLocalsStepFactory) - .factory('$initControllersStep', initControllersStepFactory) .factory('$runCanDeactivateHookStep', runCanDeactivateHookStepFactory) .factory('$runCanActivateHookStep', runCanActivateHookStepFactory) .factory('$loadTemplatesStep', loadTemplatesStepFactory) @@ -122,7 +121,7 @@ function routerFactory($$rootRouter, $rootScope, $location, $$grammar, $controll * * The value for the `ngOutlet` attribute is optional. */ -function ngOutletDirective($animate, $injector, $q, $router) { +function ngOutletDirective($animate, $injector, $q, $router, $componentMapper, $controller) { var rootRouter = $router; return { @@ -185,7 +184,20 @@ function ngOutletDirective($animate, $injector, $q, $router) { return; } - instruction.locals.$scope = newScope = scope.$new(); + var controllerConstructor = instruction.controllerConstructor; + + if (!instruction.locals.$scope) { + instruction.locals.$scope = scope.$new(); + } + newScope = instruction.locals.$scope; + + if (controllerConstructor === NOOP_CONTROLLER) { + console.warn && console.warn('Could not find controller for', $componentMapper.controllerName(instruction.component)); + } + var ctrl = $controller(controllerConstructor, instruction.locals); + instruction.controllerAs = $componentMapper.controllerAs(instruction.component); + instruction.controller = ctrl; + myCtrl.$$router = instruction.router; myCtrl.$$template = instruction.template; var controllerAs = instruction.controllerAs || instruction.component; @@ -382,25 +394,6 @@ function initLocalsStepFactory($componentMapper, $controllerIntrospector) { } } -/* - * $initControllersStep - */ -function initControllersStepFactory($controller, $componentMapper) { - return function initControllers(instruction) { - return instruction.router.traverseInstruction(instruction, function(instruction) { - var controllerConstructor = instruction.controllerConstructor; - - // if this is a string, we need to look it up... - var locals = instruction.locals; - if (controllerConstructor === NOOP_CONTROLLER) { - console.warn && console.warn('Could not find controller for', $componentMapper.controllerName(instruction.component)); - } - var ctrl = $controller(controllerConstructor, locals); - instruction.controllerAs = $componentMapper.controllerAs(instruction.component); - return instruction.controller = ctrl; - }); - } -} function runCanDeactivateHookStepFactory() { return function runCanDeactivateHook(instruction) { @@ -449,7 +442,6 @@ function pipelineProvider() { '$initLocalsStep', '$runCanDeactivateHookStep', '$runCanActivateHookStep', - '$initControllersStep', '$loadTemplatesStep', '$activateStep' ]; diff --git a/test/pipeline.es5.spec.js b/test/pipeline.es5.spec.js index 195930f..1d1371a 100644 --- a/test/pipeline.es5.spec.js +++ b/test/pipeline.es5.spec.js @@ -21,7 +21,6 @@ describe('$pipeline', function () { '$setupRoutersStep', '$initLocalsStep', 'myCustomStep', - '$initControllersStep', '$runCanDeactivateHookStep', '$runCanActivateHookStep', '$loadTemplatesStep', diff --git a/test/router-viewport.es5.spec.js b/test/router-viewport.es5.spec.js index d7dad1b..060ef4c 100644 --- a/test/router-viewport.es5.spec.js +++ b/test/router-viewport.es5.spec.js @@ -265,6 +265,25 @@ describe('ngOutlet', function () { }); + it('should inject $scope into the controller constructor', function () { + + var injectedScope; + registerComponent('user', '', function ($scope) { + injectedScope = $scope; + }); + + $router.config([ + { path: '/user', component: 'user' } + ]); + compile('
'); + + $router.navigate('/user'); + $rootScope.$digest(); + + expect(injectedScope).toBeDefined(); + }); + + it('should run the deactivate hook of controllers', function () { var spy = jasmine.createSpy('deactivate'); registerComponent('deactivate', '', {