Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: allow injecting $scope into controller constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Apr 22, 2015
1 parent b49f2ca commit 5a2441f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
38 changes: 15 additions & 23 deletions src/router-directive.es5.js
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -449,7 +442,6 @@ function pipelineProvider() {
'$initLocalsStep',
'$runCanDeactivateHookStep',
'$runCanActivateHookStep',
'$initControllersStep',
'$loadTemplatesStep',
'$activateStep'
];
Expand Down
1 change: 0 additions & 1 deletion test/pipeline.es5.spec.js
Expand Up @@ -21,7 +21,6 @@ describe('$pipeline', function () {
'$setupRoutersStep',
'$initLocalsStep',
'myCustomStep',
'$initControllersStep',
'$runCanDeactivateHookStep',
'$runCanActivateHookStep',
'$loadTemplatesStep',
Expand Down
19 changes: 19 additions & 0 deletions test/router-viewport.es5.spec.js
Expand Up @@ -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('<div ng-outlet></div>');

$router.navigate('/user');
$rootScope.$digest();

expect(injectedScope).toBeDefined();
});


it('should run the deactivate hook of controllers', function () {
var spy = jasmine.createSpy('deactivate');
registerComponent('deactivate', '', {
Expand Down

0 comments on commit 5a2441f

Please sign in to comment.