@@ -124,7 +124,8 @@ function MdCompilerProvider($compileProvider) {
124
124
* - `>=1.7` - the compiler calls the constructor first before assigning bindings and
125
125
* `$compileProvider.preAssignBindingsEnabled()` no longer exists.
126
126
*
127
- * The default value is `false` but will change to `true` in AngularJS Material 1.2.
127
+ * The default value is `false` in AngularJS 1.6 and earlier but `true` in AngularJS 1.7.
128
+ * It is planned to change this to always default to `true` in AngularJS Material 1.2.
128
129
*
129
130
* It is recommended to set this flag to `true` in AngularJS Material 1.1.x. The only reason
130
131
* it's not set that way by default is backwards compatibility. Not setting the flag to `true`
@@ -133,10 +134,13 @@ function MdCompilerProvider($compileProvider) {
133
134
* Material Dialog/Panel/Toast/BottomSheet controllers using the `$controller` helper
134
135
* as it always follows the `$compileProvider.preAssignBindingsEnabled()` value.
135
136
*/
136
- // TODO change it to `true` in Material 1.2.
137
- var respectPreAssignBindingsEnabled = false ;
137
+ var respectPreAssignBindingsEnabled = angular . version . major === 1 && angular . version . minor >= 7 ;
138
138
this . respectPreAssignBindingsEnabled = function ( respected ) {
139
139
if ( angular . isDefined ( respected ) ) {
140
+ if ( ! respected && angular . version . major === 1 && angular . version . minor >= 7 ) {
141
+ throw new Error (
142
+ 'Disabling respectPreAssignBindingsEnabled is not supported in AngularJS 1.7 or later.' ) ;
143
+ }
140
144
respectPreAssignBindingsEnabled = respected ;
141
145
return this ;
142
146
}
@@ -444,27 +448,38 @@ function MdCompilerProvider($compileProvider) {
444
448
445
449
/**
446
450
* Creates and instantiates a new controller with the specified options.
447
- * @param {!Object } options Options that include the controller
451
+ * @param {!Object } options Options that include the controller function or string.
448
452
* @param {!Object } injectLocals Locals to to be provided in the controller DI.
449
453
* @param {!Object } locals Locals to be injected to the controller.
450
454
* @returns {!Object } Created controller instance.
451
455
*/
452
456
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
455
- // 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
457
- // 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
459
- // *different instance* from `invokeCtrl()`.
460
- var invokeCtrl = this . $controller ( options . controller , injectLocals , true , options . controllerAs ) ;
461
-
462
- if ( getPreAssignBindingsEnabled ( ) && options . bindToController ) {
463
- angular . extend ( invokeCtrl . instance , locals ) ;
457
+ var ctrl ;
458
+ if ( ( angular . version . major === 1 && angular . version . minor === 7 && angular . version . dot >= 1 ) ||
459
+ ( angular . version . major === 1 && angular . version . minor > 7 ) ) {
460
+ ctrl = this . $controller ( options . controller , injectLocals ) ;
461
+ } else {
462
+ // The third argument to $controller is considered private and is undocumented
463
+ // in AngularJS prior to 1.7.1:
464
+ // https://github.com/angular/angular.js/blob/v1.6.10/src/ng/controller.js#L102-L109
465
+ // Passing `true` as the third argument causes `$controller` to return a function that
466
+ // gets the controller instance instead of returning the instance directly. When the
467
+ // controller is defined as a function, `invokeCtrl.instance` is the *same instance* as
468
+ // `invokeCtrl()`. However, when the controller is an ES6 class, `invokeCtrl.instance` is a
469
+ // *different instance* from `invokeCtrl()`.
470
+ var invokeCtrl = this . $controller ( options . controller , injectLocals , true ) ;
471
+
472
+ if ( getPreAssignBindingsEnabled ( ) && options . bindToController ) {
473
+ angular . extend ( invokeCtrl . instance , locals ) ;
474
+ }
475
+
476
+ // Instantiate and initialize the specified controller.
477
+ ctrl = invokeCtrl ( ) ;
464
478
}
465
479
466
- // Instantiate and initialize the specified controller.
467
- var ctrl = invokeCtrl ( ) ;
480
+ if ( options . controllerAs ) {
481
+ injectLocals . $scope [ options . controllerAs ] = ctrl ;
482
+ }
468
483
469
484
if ( ! getPreAssignBindingsEnabled ( ) && options . bindToController ) {
470
485
angular . extend ( ctrl , locals ) ;
0 commit comments