问题
I've created a simple directive that consists in a form with few md-input and one md-select
.
I've used my directives in a few pages now and everything works fine, but now i would like to use it inside an md-dialog
and it's not working as expected, i can't close the md-select-menu if i click outside of it, even if i focus an md-input.
So the only two ways for the user to close the menu is to either select an option or dismiss the dialog.
It's not that bad but i found this rather annoying.
Here is the content of the dialog :
<md-dialog aria-label="Modal" ng-cloak flex="75">
<md-toolbar ng-class="md-primary">
<div class="md-toolbar-tools">
<h2 translate>personModal.update</h2>
<span flex></span>
<md-button class="md-icon-button" ng-click="cancel()">
<md-icon md-svg-src="img/ic_close_white.svg" aria-label="Close dialog"></md-icon>
</md-button>
</div>
</md-toolbar>
<person-form person="person"></person-form>
</md-dialog>
And the directive content :
<form name="createPersonForm" layout="row" layout-align="space-between center" class="bg-sec query-fields">
<md-input-container flex class="full-width-input">
<label translate>createPerson.form.firstname</label>
<input name="firstname" ng-model="person.firstname" required md-asterisk/>
<div ng-messages="createPersonForm.firstname.$error"
ng-show="createPersonForm.firstname.$touched">
<div ng-message="required" translate>
createPersonForm.errors.firstnameMissing
</div>
</div>
</md-input-container>
<!-- -->
<!-- Other md-inputs here -->
<!-- -->
<md-input-container flex class="consumption-filter full-width-input">
<label translate>createPerson.form.contact</label>
<md-select name="contact" ng-model="person.contactId" required>
<md-option ng-repeat="contact in contacts" ng-value="{{contact.id}}">
<span>{{contact.name}}</span>
</md-option>
</md-select>
<div class="md-errors-spacer"></div>
<div ng-messages="createPersonForm.contact.$error"
ng-show="createPersonForm.contact.$touched">
<div ng-message="required" translate>
createPersonForm.errors.contactMissing
</div>
</div>
</md-input-container>
<md-button class="md-raised bg-white" ng-click="createPerson()" ng-disabled="createPersonForm.$invalid" ng-hide="loading" ><span translate>createPersonForm.errors.createButton</span></md-button>
<md-progress-circular md-mode="indeterminate" class="md-accent" ng-show="loading"></md-progress-circular>
</form>
I'm using Angular 1.5 and Angular Material 1.0 .
I tried toying around with z-index, and upgrading angular/angular material, but it doesn't solve the problem.
I'm considering closing the menu programatically when the md-select looses focus, but i found that kinda ugly. And i don't know how do to do that yet.
Thanks in advance !
回答1:
Using the method proposed by Freego above throws an error for me on angular-material 1.1.5
TypeError: Cannot read property '$viewValue' of undefined
I got this fixed in angular-material 1.1.5 by setting the z-index for <md-backdrop />
alongside .md-select-menu-container
.
.md-select-menu-container {
z-index: 900;
}
md-backdrop.md-select-backdrop {
z-index: 899;
}
回答2:
For those interested here is what i've done to fix my problem. I know it's not the most elegant solution, but it worked for me. If anyone has a better idea feel free to share with us !
HTML :
<md-input-container flex>
<label>Category</label>
<md-select ng-model="selectedCategory" md-on-open="dirtyFix()">
<md-option ng-repeat="cat in categories" ng-value="{{cat}}" ng-selected="$first">{{cat.name}}
</md-option>
</md-select>
</md-input-container>
JS :
$scope.dirtyFix= function () {
$timeout(function () {
$(".md-scroll-mask").css("z-index", 899);
$(".md-scroll-mask").click(function () {
$(".md-select-menu-container").remove();
$(".md-scroll-mask").css("z-index", -1);
});
}, 500);
}
CSS :
.md-select-menu-container {
z-index: 900;
}
Had to set a timeout to let the modal render and then toy with z-index and clicklistener.
It's far from perfect but maybe it'll inspire you !
来源:https://stackoverflow.com/questions/42908761/angular-material-md-select-in-md-dialog-not-closing