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

Commit 6541a55

Browse files
feat($compile): add preAssignBindingsEnabled option
A new option to enable/disable whether directive controllers are assigned bindings before calling the controller's constructor. If enabled (true), the compiler assigns the value of each of the bindings to the properties of the controller object before the constructor of this object is called. If disabled (false), the compiler calls the constructor first before assigning bindings. The default value is enabled (true) in Angular 1.5.x but will switch to false in Angular 1.6.x. See #14580 Closes #15095
1 parent ed92838 commit 6541a55

File tree

2 files changed

+5320
-5094
lines changed

2 files changed

+5320
-5094
lines changed

src/ng/compile.js

+50-14
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,35 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13691369
return debugInfoEnabled;
13701370
};
13711371

1372+
/**
1373+
* @ngdoc method
1374+
* @name $compileProvider#preAssignBindingsEnabled
1375+
*
1376+
* @param {boolean=} enabled update the preAssignBindingsEnabled state if provided, otherwise just return the
1377+
* current preAssignBindingsEnabled state
1378+
* @returns {*} current value if used as getter or itself (chaining) if used as setter
1379+
*
1380+
* @kind function
1381+
*
1382+
* @description
1383+
* Call this method to enable/disable whether directive controllers are assigned bindings before
1384+
* calling the controller's constructor.
1385+
* If enabled (true), the compiler assigns the value of each of the bindings to the
1386+
* properties of the controller object before the constructor of this object is called.
1387+
*
1388+
* If disabled (false), the compiler calls the constructor first before assigning bindings.
1389+
*
1390+
* The default value is true in Angular 1.5.x but will switch to false in Angular 1.6.x.
1391+
*/
1392+
var preAssignBindingsEnabled = true;
1393+
this.preAssignBindingsEnabled = function(enabled) {
1394+
if (isDefined(enabled)) {
1395+
preAssignBindingsEnabled = enabled;
1396+
return this;
1397+
}
1398+
return preAssignBindingsEnabled;
1399+
};
1400+
13721401

13731402
var TTL = 10;
13741403
/**
@@ -2656,22 +2685,29 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
26562685
var controller = elementControllers[name];
26572686
var bindings = controllerDirective.$$bindings.bindToController;
26582687

2659-
if (controller.identifier && bindings) {
2660-
controller.bindingInfo =
2661-
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
2662-
} else {
2663-
controller.bindingInfo = {};
2664-
}
2688+
if (preAssignBindingsEnabled) {
2689+
if (controller.identifier && bindings) {
2690+
controller.bindingInfo =
2691+
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
2692+
} else {
2693+
controller.bindingInfo = {};
2694+
}
26652695

2666-
var controllerResult = controller();
2667-
if (controllerResult !== controller.instance) {
2668-
// If the controller constructor has a return value, overwrite the instance
2669-
// from setupControllers
2670-
controller.instance = controllerResult;
2671-
$element.data('$' + controllerDirective.name + 'Controller', controllerResult);
2672-
if (controller.bindingInfo.removeWatches) {
2673-
controller.bindingInfo.removeWatches();
2696+
var controllerResult = controller();
2697+
if (controllerResult !== controller.instance) {
2698+
// If the controller constructor has a return value, overwrite the instance
2699+
// from setupControllers
2700+
controller.instance = controllerResult;
2701+
$element.data('$' + controllerDirective.name + 'Controller', controllerResult);
2702+
if (controller.bindingInfo.removeWatches) {
2703+
controller.bindingInfo.removeWatches();
2704+
}
2705+
controller.bindingInfo =
2706+
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
26742707
}
2708+
} else {
2709+
controller.instance = controller();
2710+
$element.data('$' + controllerDirective.name + 'Controller', controller.instance);
26752711
controller.bindingInfo =
26762712
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
26772713
}

0 commit comments

Comments
 (0)