Angular-Material:md-select in md-dialog not closing

时间:2017-03-20 16:19:55

标签: javascript angularjs angular-material mddialog md-select

我创建了一个简单的指令,该指令包含一个md-input和md-select很少的表单。

我现在已经在几页中使用了我的指令,一切正常,但现在我想在md-dialog内使用它并且它没有按预期工作,我可以&# 39;如果我在它外面点击,即使我关注md输入,也要关闭md-select菜单。

因此,用户关闭菜单的唯一两种方法是选择一个选项或关闭对话框。

它并没有那么糟糕,但我觉得这很烦人。

以下是对话框的内容:

<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>

指令内容:

<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>

我使用的是Angular 1.5和Angular Material 1.0。

我尝试使用z-index和升级角度/角度材料,但它并没有解决问题。

我考虑在md-select失去焦点时以编程方式关闭菜单,但我觉得有点难看。我还不知道该怎么做。

提前致谢!

2 个答案:

答案 0 :(得分:3)

使用Freego提出的方法在角度材料1.1.5上给我一个错误

TypeError: Cannot read property '$viewValue' of undefined

我通过在<md-backdrop />旁设置.md-select-menu-container的z-index,在角度材料1.1.5中修复此问题。

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

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

答案 1 :(得分:0)

对于那些感兴趣的人来说,我已经采取了哪些措施来解决我的问题。我知道它不是最优雅的解决方案,但它对我有用。如果有人有更好的想法,请随时与我们分享!

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;
}

必须设置超时以让模态渲染,然后使用z-index和clicklistener玩具。

它远非完美,但它可能会激励你!