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

Commit fa997b9

Browse files
mgoltinayuangao
authored andcommitted
feat(compiler): respect preAssignBindingsEnabled state (#10726)
1. The `$mdCompiler` is now able to respect the `preAssignBindingsEnabled` state when using AngularJS 1.5.10 or higher; to enable, invoke: $mdCompilerProvider.respectPreAssignBindingsEnabled(true); 2. `$mdCompiler` now understands the `$onInit` lifecycle hooks in controllers. Note that no other AngularJS 1.5+ lifecycle hooks are supported currently. Invoking: $mdCompilerProvider.respectPreAssignBindingsEnabled(true); will make bindings in Material custom components like `$mdDialog` or `$mdToast` only available in controller constructors if they are available in constructors of all other AngularJS controllers. By default this will happen in AngularJS 1.6 or newer. This can also be achieved with AngularJS >=1.5.10 <1.6.0 if: $compilerProvider.preAssignBindingsEnabled(false); is invoked. Example: ```js $mdDialog.show({ locals: { myVar: true }, controller: MyController, bindToController: true } function MyController() { // No locals from Angular Material are set yet. e.g myVar is undefined. } MyController.prototype.$onInit = function() { // Bindings are now set in the $onInit lifecycle hook. } ``` Huge thanks to Paul Gschwendtner (@devversion) for the initial draft version of this feature. Fixes #10016 Ref #10469 Closes #10726
1 parent 72f930b commit fa997b9

File tree

6 files changed

+526
-255
lines changed

6 files changed

+526
-255
lines changed

config/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module.exports = function(config) {
4949
basePath: __dirname + '/..',
5050
frameworks: ['jasmine'],
5151
files: dependencies.concat(testSrc),
52+
customLaunchers: require('./sauce-browsers.json'),
5253

5354
browserDisconnectTimeout:500,
5455

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
"jquery": "^3.0.0",
6262
"jshint": "^2.9.2",
6363
"jshint-summary": "^0.4.0",
64-
"karma": "^1.0.0",
64+
"karma": "^1.7.0",
65+
"karma-browserstack-launcher": "^1.2.0",
6566
"karma-chrome-launcher": "^2.0.0",
6667
"karma-firefox-launcher": "^1.0.0",
6768
"karma-jasmine": "^1.0.2",
@@ -84,4 +85,4 @@
8485
"merge-base": "git merge-base $(npm run -s current-branch) origin/master",
8586
"squash": "git rebase -i $(npm run -s merge-base)"
8687
}
87-
}
88+
}

src/components/dialog/dialog.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ function MdDialogProvider($$interimElementProvider) {
603603
});
604604

605605
/* @ngInject */
606-
function advancedDialogOptions($mdDialog, $mdConstant) {
606+
function advancedDialogOptions() {
607607
return {
608608
template: [
609609
'<md-dialog md-theme="{{ dialog.theme || dialog.defaultTheme }}" aria-label="{{ dialog.ariaLabel }}" ng-class="dialog.css">',
@@ -631,30 +631,40 @@ function MdDialogProvider($$interimElementProvider) {
631631
' </md-dialog-actions>',
632632
'</md-dialog>'
633633
].join('').replace(/\s\s+/g, ''),
634-
controller: function mdDialogCtrl() {
635-
var isPrompt = this.$type == 'prompt';
636-
637-
if (isPrompt && this.initialValue) {
638-
this.result = this.initialValue;
639-
}
640-
641-
this.hide = function() {
642-
$mdDialog.hide(isPrompt ? this.result : true);
643-
};
644-
this.abort = function() {
645-
$mdDialog.cancel();
646-
};
647-
this.keypress = function($event) {
648-
if ($event.keyCode === $mdConstant.KEY_CODE.ENTER) {
649-
$mdDialog.hide(this.result);
650-
}
651-
};
652-
},
634+
controller: MdDialogController,
653635
controllerAs: 'dialog',
654636
bindToController: true,
655637
};
656638
}
657639

640+
/**
641+
* Controller for the md-dialog interim elements
642+
* @ngInject
643+
*/
644+
function MdDialogController($mdDialog, $mdConstant) {
645+
// For compatibility with AngularJS 1.6+, we should always use the $onInit hook in
646+
// interimElements. The $mdCompiler simulates the $onInit hook for all versions.
647+
this.$onInit = function() {
648+
var isPrompt = this.$type == 'prompt';
649+
650+
if (isPrompt && this.initialValue) {
651+
this.result = this.initialValue;
652+
}
653+
654+
this.hide = function() {
655+
$mdDialog.hide(isPrompt ? this.result : true);
656+
};
657+
this.abort = function() {
658+
$mdDialog.cancel();
659+
};
660+
this.keypress = function($event) {
661+
if ($event.keyCode === $mdConstant.KEY_CODE.ENTER) {
662+
$mdDialog.hide(this.result);
663+
}
664+
};
665+
};
666+
}
667+
658668
/* @ngInject */
659669
function dialogDefaultOptions($mdDialog, $mdAria, $mdUtil, $mdConstant, $animate, $document, $window, $rootElement,
660670
$log, $injector, $mdTheming, $interpolate, $mdInteraction) {

src/components/toast/toast.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -321,24 +321,7 @@ function MdToastProvider($$interimElementProvider) {
321321
' </md-button>' +
322322
' </div>' +
323323
'</md-toast>',
324-
controller: /* @ngInject */ function mdToastCtrl($scope) {
325-
var self = this;
326-
327-
if (self.highlightAction) {
328-
$scope.highlightClasses = [
329-
'md-highlight',
330-
self.highlightClass
331-
]
332-
}
333-
334-
$scope.$watch(function() { return activeToastContent; }, function() {
335-
self.content = activeToastContent;
336-
});
337-
338-
this.resolve = function() {
339-
$mdToast.hide( ACTION_RESOLVE );
340-
};
341-
},
324+
controller: MdToastController,
342325
theme: $mdTheming.defaultTheme(),
343326
controllerAs: 'toast',
344327
bindToController: true
@@ -354,6 +337,33 @@ function MdToastProvider($$interimElementProvider) {
354337

355338
return $mdToast;
356339

340+
/**
341+
* Controller for the Toast interim elements.
342+
* @ngInject
343+
*/
344+
function MdToastController($mdToast, $scope) {
345+
// For compatibility with AngularJS 1.6+, we should always use the $onInit hook in
346+
// interimElements. The $mdCompiler simulates the $onInit hook for all versions.
347+
this.$onInit = function() {
348+
var self = this;
349+
350+
if (self.highlightAction) {
351+
$scope.highlightClasses = [
352+
'md-highlight',
353+
self.highlightClass
354+
]
355+
}
356+
357+
$scope.$watch(function() { return activeToastContent; }, function() {
358+
self.content = activeToastContent;
359+
});
360+
361+
this.resolve = function() {
362+
$mdToast.hide( ACTION_RESOLVE );
363+
};
364+
}
365+
}
366+
357367
/* @ngInject */
358368
function toastDefaultOptions($animate, $mdToast, $mdUtil, $mdMedia) {
359369
var SWIPE_EVENTS = '$md.swipeleft $md.swiperight $md.swipeup $md.swipedown';

0 commit comments

Comments
 (0)