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

Commit dfb8cf6

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 de1ede7 commit dfb8cf6

File tree

2 files changed

+5401
-5176
lines changed

2 files changed

+5401
-5176
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
/**
@@ -2679,22 +2708,29 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
26792708
var controller = elementControllers[name];
26802709
var bindings = controllerDirective.$$bindings.bindToController;
26812710

2682-
if (controller.identifier && bindings) {
2683-
controller.bindingInfo =
2684-
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
2685-
} else {
2686-
controller.bindingInfo = {};
2687-
}
2711+
if (preAssignBindingsEnabled) {
2712+
if (controller.identifier && bindings) {
2713+
controller.bindingInfo =
2714+
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
2715+
} else {
2716+
controller.bindingInfo = {};
2717+
}
26882718

2689-
var controllerResult = controller();
2690-
if (controllerResult !== controller.instance) {
2691-
// If the controller constructor has a return value, overwrite the instance
2692-
// from setupControllers
2693-
controller.instance = controllerResult;
2694-
$element.data('$' + controllerDirective.name + 'Controller', controllerResult);
2695-
if (controller.bindingInfo.removeWatches) {
2696-
controller.bindingInfo.removeWatches();
2719+
var controllerResult = controller();
2720+
if (controllerResult !== controller.instance) {
2721+
// If the controller constructor has a return value, overwrite the instance
2722+
// from setupControllers
2723+
controller.instance = controllerResult;
2724+
$element.data('$' + controllerDirective.name + 'Controller', controllerResult);
2725+
if (controller.bindingInfo.removeWatches) {
2726+
controller.bindingInfo.removeWatches();
2727+
}
2728+
controller.bindingInfo =
2729+
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
26972730
}
2731+
} else {
2732+
controller.instance = controller();
2733+
$element.data('$' + controllerDirective.name + 'Controller', controller.instance);
26982734
controller.bindingInfo =
26992735
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
27002736
}

0 commit comments

Comments
 (0)