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

Commit 7edda11

Browse files
rschmuklerjelbourn
authored andcommitted
fix(select): fix bugs introduced by keeping md-select-menu in DOM. Fix tests
Closes #6071
1 parent ef05ea3 commit 7edda11

File tree

2 files changed

+75
-96
lines changed

2 files changed

+75
-96
lines changed

src/components/select/select.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
418418
selectContainer[0].setAttribute('class', value);
419419
}
420420
selectMenuCtrl = selectContainer.find('md-select-menu').controller('mdSelectMenu');
421+
selectMenuCtrl.init(ngModelCtrl, attr.ngModel);
421422
}
422423

423424
function handleKeypress(e) {
@@ -452,6 +453,7 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
452453
element: selectContainer,
453454
target: element[0],
454455
preserveElement: true,
456+
parent: element,
455457
hasBackdrop: true,
456458
loadingAsync: attr.mdOnOpen ? scope.$eval(attr.mdOnOpen) || true : false
457459
}).finally(function() {
@@ -468,7 +470,7 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) {
468470

469471
return {
470472
restrict: 'E',
471-
require: ['mdSelectMenu', '^ngModel'],
473+
require: ['mdSelectMenu'],
472474
scope: true,
473475
controller: SelectMenuController,
474476
link: {pre: preLink}
@@ -478,12 +480,10 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) {
478480
// its child options run postLink.
479481
function preLink(scope, element, attr, ctrls) {
480482
var selectCtrl = ctrls[0];
481-
var ngModel = ctrls[1];
482483

483484
$mdTheming(element);
484485
element.on('click', clickListener);
485486
element.on('keypress', keyListener);
486-
if (ngModel) selectCtrl.init(ngModel);
487487

488488
function keyListener(e) {
489489
if (e.keyCode == 13 || e.keyCode == 32) {
@@ -551,7 +551,7 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) {
551551

552552
// watchCollection on the model because by default ngModel only watches the model's
553553
// reference. This allowed the developer to also push and pop from their array.
554-
$scope.$watchCollection($attrs.ngModel, function(value) {
554+
$scope.$watchCollection(self.modelBinding, function(value) {
555555
if (validateArray(value)) renderMultiple(value);
556556
self.ngModel.$setPristine();
557557
});
@@ -598,8 +598,9 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) {
598598
}
599599
};
600600

601-
self.init = function(ngModel) {
601+
self.init = function(ngModel, binding) {
602602
self.ngModel = ngModel;
603+
self.modelBinding = binding;
603604

604605
// Allow users to provide `ng-model="foo" ng-model-options="{trackBy: 'foo.id'}"` so
605606
// that we can properly compare objects set on the model to the available options
@@ -782,7 +783,15 @@ function OptionDirective($mdButtonInkRipple, $mdUtil) {
782783
$mdButtonInkRipple.attach(scope, element);
783784
configureAria();
784785

785-
function setOptionValue(newValue, oldValue) {
786+
function setOptionValue(newValue, oldValue, prevAttempt) {
787+
if (!selectCtrl.hashGetter) {
788+
if (!prevAttempt) {
789+
scope.$$postDigest(function() {
790+
setOptionValue(newValue, oldValue, true);
791+
});
792+
}
793+
return;
794+
}
786795
var oldHashKey = selectCtrl.hashGetter(oldValue, scope);
787796
var newHashKey = selectCtrl.hashGetter(newValue, scope);
788797

@@ -1238,7 +1247,7 @@ function SelectProvider($$interimElementProvider) {
12381247
* trigger the [optional] user-defined expression
12391248
*/
12401249
function announceClosed(opts) {
1241-
var mdSelect = opts.target.controller('mdSelect');
1250+
var mdSelect = opts.selectEl.controller('mdSelect');
12421251
if (mdSelect) {
12431252
var menuController = opts.selectEl.controller('mdSelectMenu');
12441253
mdSelect.setLabelText(menuController.selectedLabels());
@@ -1253,7 +1262,7 @@ function SelectProvider($$interimElementProvider) {
12531262
function calculateMenuPositions(scope, element, opts) {
12541263
var
12551264
containerNode = element[0],
1256-
targetNode = opts.target[0].children[0], // target the label
1265+
targetNode = opts.target[0].children[1], // target the label
12571266
parentNode = $document[0].body,
12581267
selectNode = opts.selectEl[0],
12591268
contentNode = opts.contentEl[0],

0 commit comments

Comments
 (0)