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

Commit 4f599ff

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
1 parent 0a5cf18 commit 4f599ff

File tree

2 files changed

+121
-23
lines changed

2 files changed

+121
-23
lines changed

src/ng/compile.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1371,10 +1371,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13711371

13721372
/**
13731373
* @ngdoc method
1374-
* @name $compileProvider#preAssignBindings
1374+
* @name $compileProvider#preAssignBindingsEnabled
13751375
*
1376-
* @param {boolean=} enabled update the preAssignBindings state if provided, otherwise just return the
1377-
* current preAssignBindings state
1376+
* @param {boolean=} enabled update the preAssignBindingsEnabled state if provided, otherwise just return the
1377+
* current preAssignBindingsEnabled state
13781378
* @returns {*} current value if used as getter or itself (chaining) if used as setter
13791379
*
13801380
* @kind function
@@ -1389,13 +1389,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13891389
*
13901390
* The default value is true in Angular 1.5.x but will switch to false in Angular 1.6.x.
13911391
*/
1392-
var preAssignBindings = true;
1393-
this.preAssignBindings = function(enabled) {
1392+
var preAssignBindingsEnabled = true;
1393+
this.preAssignBindingsEnabled = function(enabled) {
13941394
if (isDefined(enabled)) {
1395-
preAssignBindings = enabled;
1395+
preAssignBindingsEnabled = enabled;
13961396
return this;
13971397
}
1398-
return preAssignBindings;
1398+
return preAssignBindingsEnabled;
13991399
};
14001400

14011401

@@ -2708,7 +2708,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
27082708
var controller = elementControllers[name];
27092709
var bindings = controllerDirective.$$bindings.bindToController;
27102710

2711-
if (preAssignBindings) {
2711+
if (preAssignBindingsEnabled) {
27122712
if (controller.identifier && bindings) {
27132713
controller.bindingInfo =
27142714
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);

test/ng/compileSpec.js

+113-15
Original file line numberDiff line numberDiff line change
@@ -3845,10 +3845,10 @@ describe('$compile', function() {
38453845
});
38463846
});
38473847

3848-
forEach([true, false], function(preAssignBindings) {
3849-
describe((preAssignBindings ? 'with' : 'without') + ' pre-assigned bindings', function() {
3848+
forEach([true, false], function(preAssignBindingsEnabled) {
3849+
describe((preAssignBindingsEnabled ? 'with' : 'without') + ' pre-assigned bindings', function() {
38503850
beforeEach(module(function($compileProvider) {
3851-
$compileProvider.preAssignBindings(preAssignBindings);
3851+
$compileProvider.preAssignBindingsEnabled(preAssignBindingsEnabled);
38523852
}));
38533853

38543854
describe('controller lifecycle hooks', function() {
@@ -5716,7 +5716,7 @@ describe('$compile', function() {
57165716
expect(this.str).toBe('Hello, world!');
57175717
expect(this.fn()).toBe('called!');
57185718
};
5719-
if (preAssignBindings) {
5719+
if (preAssignBindingsEnabled) {
57205720
this.check();
57215721
} else {
57225722
this.$onInit = this.check;
@@ -5742,6 +5742,104 @@ describe('$compile', function() {
57425742
});
57435743

57445744

5745+
it('should not pre-assign bound properties to the controller if `preAssignBindingsEnabled` is disabled', function() {
5746+
var controllerCalled = false, onInitCalled = false;
5747+
module(function($compileProvider) {
5748+
$compileProvider.preAssignBindingsEnabled(false);
5749+
$compileProvider.directive('fooDir', valueFn({
5750+
template: '<p>isolate</p>',
5751+
scope: {
5752+
'data': '=dirData',
5753+
'oneway': '<dirData',
5754+
'str': '@dirStr',
5755+
'fn': '&dirFn'
5756+
},
5757+
controller: function($scope) {
5758+
expect(this.data).toBeUndefined();
5759+
expect(this.oneway).toBeUndefined();
5760+
expect(this.str).toBeUndefined();
5761+
expect(this.fn).toBeUndefined();
5762+
controllerCalled = true;
5763+
this.$onInit = function() {
5764+
expect(this.data).toEqualData({
5765+
'foo': 'bar',
5766+
'baz': 'biz'
5767+
});
5768+
expect(this.oneway).toEqualData({
5769+
'foo': 'bar',
5770+
'baz': 'biz'
5771+
});
5772+
expect(this.str).toBe('Hello, world!');
5773+
expect(this.fn()).toBe('called!');
5774+
onInitCalled = true;
5775+
};
5776+
},
5777+
controllerAs: 'test',
5778+
bindToController: true
5779+
}));
5780+
});
5781+
inject(function($compile, $rootScope) {
5782+
$rootScope.fn = valueFn('called!');
5783+
$rootScope.whom = 'world';
5784+
$rootScope.remoteData = {
5785+
'foo': 'bar',
5786+
'baz': 'biz'
5787+
};
5788+
element = $compile('<div foo-dir dir-data="remoteData" ' +
5789+
'dir-str="Hello, {{whom}}!" ' +
5790+
'dir-fn="fn()"></div>')($rootScope);
5791+
expect(controllerCalled).toBe(true);
5792+
expect(onInitCalled).toBe(true);
5793+
});
5794+
});
5795+
5796+
it('should pre-assign bound properties to the controller if `preAssignBindingsEnabled` is enabled', function() {
5797+
var controllerCalled = false, onInitCalled = false;
5798+
module(function($compileProvider) {
5799+
$compileProvider.preAssignBindingsEnabled(true);
5800+
$compileProvider.directive('fooDir', valueFn({
5801+
template: '<p>isolate</p>',
5802+
scope: {
5803+
'data': '=dirData',
5804+
'oneway': '<dirData',
5805+
'str': '@dirStr',
5806+
'fn': '&dirFn'
5807+
},
5808+
controller: function($scope) {
5809+
expect(this.data).toEqualData({
5810+
'foo': 'bar',
5811+
'baz': 'biz'
5812+
});
5813+
expect(this.oneway).toEqualData({
5814+
'foo': 'bar',
5815+
'baz': 'biz'
5816+
});
5817+
expect(this.str).toBe('Hello, world!');
5818+
expect(this.fn()).toBe('called!');
5819+
controllerCalled = true;
5820+
this.$onInit = function() {
5821+
onInitCalled = true;
5822+
};
5823+
},
5824+
controllerAs: 'test',
5825+
bindToController: true
5826+
}));
5827+
});
5828+
inject(function($compile, $rootScope) {
5829+
$rootScope.fn = valueFn('called!');
5830+
$rootScope.whom = 'world';
5831+
$rootScope.remoteData = {
5832+
'foo': 'bar',
5833+
'baz': 'biz'
5834+
};
5835+
element = $compile('<div foo-dir dir-data="remoteData" ' +
5836+
'dir-str="Hello, {{whom}}!" ' +
5837+
'dir-fn="fn()"></div>')($rootScope);
5838+
expect(controllerCalled).toBe(true);
5839+
expect(onInitCalled).toBe(true);
5840+
});
5841+
});
5842+
57455843
it('should eventually expose isolate scope variables on ES6 class controller with controllerAs when bindToController is true', function() {
57465844
if (!/chrome/i.test(window.navigator.userAgent)) return;
57475845
var controllerCalled = false;
@@ -5846,7 +5944,7 @@ describe('$compile', function() {
58465944
expect(this.str).toBe('Hello, world!');
58475945
expect(this.fn()).toBe('called!');
58485946
};
5849-
if (preAssignBindings) {
5947+
if (preAssignBindingsEnabled) {
58505948
this.check();
58515949
} else {
58525950
this.$onInit = this.check;
@@ -5993,7 +6091,7 @@ describe('$compile', function() {
59936091
expect(this.fn()).toBe('called!');
59946092
};
59956093
controllerCalled = true;
5996-
if (preAssignBindings) {
6094+
if (preAssignBindingsEnabled) {
59976095
this.check();
59986096
} else {
59996097
this.$onInit = this.check;
@@ -6045,7 +6143,7 @@ describe('$compile', function() {
60456143
expect(this.fn()).toBe('called!');
60466144
};
60476145
controllerCalled = true;
6048-
if (preAssignBindings) {
6146+
if (preAssignBindingsEnabled) {
60496147
this.check();
60506148
} else {
60516149
this.$onInit = this.check;
@@ -6100,7 +6198,7 @@ describe('$compile', function() {
61006198
expect(this.fn()).toBe('called!');
61016199
};
61026200
controller1Called = true;
6103-
if (preAssignBindings) {
6201+
if (preAssignBindingsEnabled) {
61046202
this.check();
61056203
} else {
61066204
this.$onInit = this.check;
@@ -6123,7 +6221,7 @@ describe('$compile', function() {
61236221
expect(this.fn()).toBe('second called!');
61246222
};
61256223
controller2Called = true;
6126-
if (preAssignBindings) {
6224+
if (preAssignBindingsEnabled) {
61276225
this.check();
61286226
} else {
61296227
this.$onInit = this.check;
@@ -6177,7 +6275,7 @@ describe('$compile', function() {
61776275
expect(this.fn()).toBe('called!');
61786276
};
61796277
controller1Called = true;
6180-
if (preAssignBindings) {
6278+
if (preAssignBindingsEnabled) {
61816279
this.check();
61826280
} else {
61836281
this.$onInit = this.check;
@@ -6200,7 +6298,7 @@ describe('$compile', function() {
62006298
expect(this.fn()).toBe('second called!');
62016299
};
62026300
controller2Called = true;
6203-
if (preAssignBindings) {
6301+
if (preAssignBindingsEnabled) {
62046302
this.check();
62056303
} else {
62066304
this.$onInit = this.check;
@@ -6254,7 +6352,7 @@ describe('$compile', function() {
62546352
expect(this.fn()).toBe('called!');
62556353
};
62566354
controller1Called = true;
6257-
if (preAssignBindings) {
6355+
if (preAssignBindingsEnabled) {
62586356
this.check();
62596357
} else {
62606358
this.$onInit = this.check;
@@ -6278,7 +6376,7 @@ describe('$compile', function() {
62786376
expect(this.fn()).toBe('second called!');
62796377
};
62806378
controller2Called = true;
6281-
if (preAssignBindings) {
6379+
if (preAssignBindingsEnabled) {
62826380
this.check();
62836381
} else {
62846382
this.$onInit = this.check;
@@ -6598,7 +6696,7 @@ describe('$compile', function() {
65986696
this.initProp = function() {
65996697
this.prop = this.prop || 'default';
66006698
};
6601-
if (preAssignBindings) {
6699+
if (preAssignBindingsEnabled) {
66026700
this.initProp();
66036701
} else {
66046702
this.$onInit = this.initProp;
@@ -6636,7 +6734,7 @@ describe('$compile', function() {
66366734
this.getProp = function() {
66376735
return self.prop;
66386736
};
6639-
if (preAssignBindings) {
6737+
if (preAssignBindingsEnabled) {
66406738
this.initProp();
66416739
} else {
66426740
this.$onInit = this.initProp;

0 commit comments

Comments
 (0)