@@ -102,9 +102,9 @@ function MdCompilerProvider($compileProvider) {
102
102
* @name $mdCompilerProvider#respectPreAssignBindingsEnabled
103
103
*
104
104
* @param {boolean= } respected update the `respectPreAssignBindingsEnabled` state if provided,
105
- * otherwise just return the current Material `respectPreAssignBindingsEnabled` state.
106
- * @returns {boolean|MdCompilerProvider } current value if used as getter or itself (chaining)
107
- * if used as setter
105
+ * otherwise just return the current Material `respectPreAssignBindingsEnabled` state.
106
+ * @returns {boolean|MdCompilerProvider } current value, if used as a getter, or itself (chaining)
107
+ * if used as a setter.
108
108
*
109
109
* @description
110
110
* Call this method to enable/disable whether Material-specific (dialog/panel/toast/bottomsheet)
@@ -114,26 +114,36 @@ function MdCompilerProvider($compileProvider) {
114
114
*
115
115
* If disabled (`false`), the compiler assigns the value of each of the bindings to the
116
116
* properties of the controller object before the constructor of this object is called.
117
+ * The ability to disable this settings is **deprecated** and will be removed in
118
+ * AngularJS Material 1.2.0.
117
119
*
118
120
* If enabled (`true`) the behavior depends on the AngularJS version used:
119
121
*
120
- * - `<1.5.10` - bindings are pre-assigned.
121
- * - `>=1.5.10 <1.7` - behaves like set to whatever `$compileProvider.preAssignBindingsEnabled()` reports.
122
- * If the `preAssignBindingsEnabled` flag wasn't set manually, it defaults to pre-assigning bindings
123
- * with AngularJS `1.5.x` and to calling the constructor first with AngularJS `1.6.x`.
124
- * - `>=1.7` - the compiler calls the constructor first before assigning bindings and
122
+ * - `<1.5.10`
123
+ * - Bindings are pre-assigned.
124
+ * - `>=1.5.10 <1.7`
125
+ * - Respects whatever `$compileProvider.preAssignBindingsEnabled()` reports. If the
126
+ * `preAssignBindingsEnabled` flag wasn't set manually, it defaults to pre-assigning bindings
127
+ * with AngularJS `1.5` and to calling the constructor first with AngularJS `1.6`.
128
+ * - `>=1.7`
129
+ * - The compiler calls the constructor first before assigning bindings and
125
130
* `$compileProvider.preAssignBindingsEnabled()` no longer exists.
126
131
*
127
- * The default value is `false` but will change to `true` in AngularJS Material 1.2.
132
+ * Defaults
133
+ * - The default value is `false` in AngularJS 1.6 and earlier.
134
+ * - It is planned to fix this value to `true` and not allow the `false` value in
135
+ * AngularJS Material 1.2.0.
128
136
*
129
- * It is recommended to set this flag to `true` in AngularJS Material 1.1.x. The only reason
130
- * it's not set that way by default is backwards compatibility. Not setting the flag to `true`
131
- * when AngularJS' `$compileProvider.preAssignBindingsEnabled()` is set to `false`
132
- * (i.e. default behavior in AngularJS 1.6 or newer) makes it hard to unit test
137
+ * It is recommended to set this flag to `true` when using AngularJS Material 1.1.x with
138
+ * AngularJS versions >= 1.5.10. The only reason it's not set that way by default is backwards
139
+ * compatibility.
140
+ *
141
+ * By not setting the flag to `true` when AngularJS' `$compileProvider.preAssignBindingsEnabled()`
142
+ * is set to `false` (i.e. default behavior in AngularJS 1.6 or newer), unit testing of
133
143
* Material Dialog/Panel/Toast/BottomSheet controllers using the `$controller` helper
134
- * as it always follows the `$compileProvider.preAssignBindingsEnabled()` value.
144
+ * is problematic as it always follows AngularJS' `$compileProvider.preAssignBindingsEnabled()`
145
+ * value.
135
146
*/
136
- // TODO change it to `true` in Material 1.2.
137
147
var respectPreAssignBindingsEnabled = false ;
138
148
this . respectPreAssignBindingsEnabled = function ( respected ) {
139
149
if ( angular . isDefined ( respected ) ) {
@@ -147,11 +157,11 @@ function MdCompilerProvider($compileProvider) {
147
157
/**
148
158
* @private
149
159
* @description
150
- * This function returns `true` if AngularJS Material-specific (dialog/panel/toast/bottomsheet) controllers have
151
- * bindings pre-assigned in controller constructors and `false` otherwise.
160
+ * This function returns `true` if AngularJS Material-specific (dialog/panel/toast/bottomsheet)
161
+ * controllers have bindings pre-assigned in controller constructors and `false` otherwise.
152
162
*
153
- * Note that this doesn't affect directives/components created via regular AngularJS methods which constitute most
154
- * Material and user-created components; their behavior can be checked via
163
+ * Note that this doesn't affect directives/components created via regular AngularJS methods
164
+ * which constitute most Material and user-created components; their behavior can be checked via
155
165
* `$compileProvider.preAssignBindingsEnabled()` in AngularJS `>=1.5.10 <1.7.0`.
156
166
*
157
167
* @returns {* } current preAssignBindingsEnabled state
@@ -444,30 +454,43 @@ function MdCompilerProvider($compileProvider) {
444
454
445
455
/**
446
456
* Creates and instantiates a new controller with the specified options.
447
- * @param {!Object } options Options that include the controller
457
+ * @param {!Object } options Options that include the controller function or string.
448
458
* @param {!Object } injectLocals Locals to to be provided in the controller DI.
449
459
* @param {!Object } locals Locals to be injected to the controller.
450
460
* @returns {!Object } Created controller instance.
451
461
*/
452
462
MdCompilerService . prototype . _createController = function ( options , injectLocals , locals ) {
453
- // The third and fourth arguments to $controller are considered private and are undocumented:
454
- // https://github.com/angular/angular.js/blob/master/src/ng/controller.js#L86
463
+ var ctrl ;
464
+ var preAssignBindingsEnabled = getPreAssignBindingsEnabled ( ) ;
465
+ // The third argument to $controller is considered private and undocumented:
466
+ // https://github.com/angular/angular.js/blob/v1.6.10/src/ng/controller.js#L102-L109.
467
+ // TODO remove the use of this third argument in AngularJS Material 1.2.0.
455
468
// Passing `true` as the third argument causes `$controller` to return a function that
456
- // gets the controller instance instead returning of the instance directly. When the
469
+ // gets the controller instance instead of returning the instance directly. When the
457
470
// controller is defined as a function, `invokeCtrl.instance` is the *same instance* as
458
- // `invokeCtrl()`. However, then the controller is an ES6 class, `invokeCtrl.instance` is a
471
+ // `invokeCtrl()`. However, when the controller is an ES6 class, `invokeCtrl.instance` is a
459
472
// *different instance* from `invokeCtrl()`.
460
- var invokeCtrl = this . $controller ( options . controller , injectLocals , true , options . controllerAs ) ;
473
+ if ( preAssignBindingsEnabled ) {
474
+ var invokeCtrl = this . $controller ( options . controller , injectLocals , true ) ;
461
475
462
- if ( getPreAssignBindingsEnabled ( ) && options . bindToController ) {
463
- angular . extend ( invokeCtrl . instance , locals ) ;
464
- }
476
+ if ( options . bindToController ) {
477
+ angular . extend ( invokeCtrl . instance , locals ) ;
478
+ }
465
479
466
- // Instantiate and initialize the specified controller.
467
- var ctrl = invokeCtrl ( ) ;
480
+ // Use the private API callback to instantiate and initialize the specified controller.
481
+ ctrl = invokeCtrl ( ) ;
482
+ } else {
483
+ // If we don't need to pre-assign bindings, avoid using the private API third argument and
484
+ // related callback.
485
+ ctrl = this . $controller ( options . controller , injectLocals ) ;
486
+
487
+ if ( options . bindToController ) {
488
+ angular . extend ( ctrl , locals ) ;
489
+ }
490
+ }
468
491
469
- if ( ! getPreAssignBindingsEnabled ( ) && options . bindToController ) {
470
- angular . extend ( ctrl , locals ) ;
492
+ if ( options . controllerAs ) {
493
+ injectLocals . $scope [ options . controllerAs ] = ctrl ;
471
494
}
472
495
473
496
// Call the $onInit hook if it's present on the controller.
0 commit comments