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

Commit 81be416

Browse files
committed
refactor($compile): move setting of controller data to single location
1 parent 7ffb2d3 commit 81be416

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/ng/compile.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -2086,17 +2086,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20862086
controller = attrs[directive.name];
20872087
}
20882088

2089-
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
2090-
2091-
// For directives with element transclusion the element is a comment,
2092-
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
2093-
// clean up (http://bugs.jquery.com/ticket/8335).
2094-
// Instead, we save the controllers for the element in a local hash and attach to .data
2095-
// later, once we have the actual element.
2096-
elementControllers[directive.name] = controllerInstance;
2097-
if (!hasElementTranscludeDirective) {
2098-
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
2099-
}
2089+
elementControllers[directive.name] = $controller(controller, locals, true, directive.controllerAs);
21002090
}
21012091
return elementControllers;
21022092
}
@@ -2146,12 +2136,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
21462136
}
21472137
}
21482138

2149-
// Initialize bindToController bindings
2139+
// Initialize controllers
21502140
for (var name in elementControllers) {
21512141
var controllerDirective = controllerDirectives[name];
21522142
var controller = elementControllers[name];
21532143
var bindings = controllerDirective.$$bindings.bindToController;
21542144

2145+
// Initialize bindToController bindings
21552146
if (controller.identifier && bindings) {
21562147
removeControllerBindingWatches =
21572148
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
@@ -2162,11 +2153,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
21622153
// If the controller constructor has a return value, overwrite the instance
21632154
// from setupControllers
21642155
controller.instance = controllerResult;
2165-
$element.data('$' + controllerDirective.name + 'Controller', controllerResult);
21662156
removeControllerBindingWatches && removeControllerBindingWatches();
21672157
removeControllerBindingWatches =
21682158
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
21692159
}
2160+
2161+
// Store controllers on the $element data
2162+
// For transclude comment nodes this will be a noop and will be done at transclusion time
2163+
$element.data('$' + controllerDirective.name + 'Controller', controllerResult);
21702164
}
21712165

21722166
// PRELINKING

0 commit comments

Comments
 (0)