简体   繁体   English

Angular-Material:md-dialog 中的 md-select 未关闭

[英]Angular-Material : md-select in md-dialog not closing

I've created a simple directive that consists in a form with few md-input and one md-select .我创建了一个简单的指令,它包含在一个带有几个 md-input 和一个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.我现在在几页中使用了我的指令,一切正常,但现在我想在md-dialog使用它,但它没有按预期工作,如果我点击,我无法关闭 md-select-menu在它之外,即使我专注于 md 输入。

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 .我正在使用 Angular 1.5 和 Angular Material 1.0 。

I tried toying around with z-index, and upgrading angular/angular material, but it doesn't solve the problem.我尝试玩弄 z-index,并升级 angular/angular 材料,但这并没有解决问题。

I'm considering closing the menu programatically when the md-select looses focus, but i found that kinda ugly.我正在考虑在 md-select 失去焦点时以编程方式关闭菜单,但我发现这有点难看。 And i don't know how do to do that yet.我还不知道该怎么做。

Thanks in advance !提前致谢 !

Using the method proposed by Freego above throws an error for me on angular-material 1.1.5使用上面 Freego 提出的方法在 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 .我在 angular-material 1.1.5 中通过为<md-backdrop />.md-select-menu-container设置 z-index 来解决这个问题。

.md-select-menu-container {
    z-index: 900;
}

md-backdrop.md-select-backdrop {
    z-index: 899;
}

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 : 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 : 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 : 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.必须设置超时才能让模态渲染,然后使用 z-index 和 clicklistener。

It's far from perfect but maybe it'll inspire you !它远非完美,但也许它会激励你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM