Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 9f148d2

Browse files
committed
refactor($compile): move setting of controller data to single location
1 parent 4883e95 commit 9f148d2

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/ng/compile.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -2319,12 +2319,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
23192319
}
23202320
}
23212321

2322-
// Initialize bindToController bindings
2322+
// Initialize controllers
23232323
for (var name in elementControllers) {
23242324
var controllerDirective = controllerDirectives[name];
23252325
var controller = elementControllers[name];
23262326
var bindings = controllerDirective.$$bindings.bindToController;
23272327

2328+
// Initialize bindToController bindings
23282329
if (controller.identifier && bindings) {
23292330
removeControllerBindingWatches =
23302331
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
@@ -2335,11 +2336,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
23352336
// If the controller constructor has a return value, overwrite the instance
23362337
// from setupControllers
23372338
controller.instance = controllerResult;
2338-
$element.data('$' + controllerDirective.name + 'Controller', controllerResult);
23392339
removeControllerBindingWatches && removeControllerBindingWatches();
23402340
removeControllerBindingWatches =
23412341
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
23422342
}
2343+
2344+
// Store controllers on the $element data
2345+
// For transclude comment nodes this will be a noop and will be done at transclusion time
2346+
$element.data('$' + controllerDirective.name + 'Controller', controllerResult);
23432347
}
23442348

23452349
// Bind the required controllers to the controller, if `require` is an object and `bindToController` is truthy
@@ -2489,14 +2493,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
24892493
controller = attrs[directive.name];
24902494
}
24912495

2492-
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
2493-
2494-
// For directives with element transclusion the element is a comment.
2495-
// In this case .data will not attach any data.
2496-
// Instead, we save the controllers for the element in a local hash and attach to .data
2497-
// later, once we have the actual element.
2498-
elementControllers[directive.name] = controllerInstance;
2499-
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
2496+
elementControllers[directive.name] = $controller(controller, locals, true, directive.controllerAs);
25002497
}
25012498
return elementControllers;
25022499
}

0 commit comments

Comments
 (0)