使用角度材料防止ng-click事件

时间:2017-02-10 15:14:43

标签: angularjs angular-material

我在自定义角度下拉列表中有一系列切换。我遇到的问题是,切换切换,但他们也关闭下拉列表。我怎么能阻止这个?双击抽屉时,下拉列表仍应关闭,这是预期的行为,但在单击切换时则不然。

这是HTML:

<div class="cnt" ng-repeat="item in data">
    <div class="menu-item" ng-click="toggle(item.pos);">
        <md-list layout="row" layout-padding="" class="layout-row" layout-align="start center" flex> 
            <span class="title flex" flex=""> {{item.name}}</span>
            <i class="fa fa-chevron-down" ng-class="{'rotate180': item.pos==menuIsOpen, 'rotate-back': !menuIsOpen}"></i>
        </md-list>

        <div class="sub-menu" ng-animate="'animate'" >
            <div ng-repeat='(k,v) in item track by $index'>
                <div ng-if="isArray(v)">
                    <md-menu-item ng-if="menuIsOpen===item.pos" ng-repeat='v1 in v track by $index'>                        
                        <md-button>
                            <div layout="row" flex="">
                                <md-switch class="md-primary" name="special" ng-model="project.special" required>

                                <span class="">
                                    <p flex="">{{v1.title}}</p>
                                </span>
                              </md-switch>
                            </div>
                        </md-button>    
                    </md-menu-item>
                </div>
            </div>              
        </div> 
    </div>
</div>

并且控制器将其用于功能目的和来自json的数据填充:

$scope.toggle = function(itemPos) {
    if ($scope.menuIsOpen === itemPos) {
        $scope.menuIsOpen = 0; 
    }
    else {
        $scope.menuIsOpen = itemPos;  
    }
}
$scope.isArray = function(val) {
    return Array.isArray(val);
}

扰流警报:向md-prevent-menu-close="true"添加md-button没有帮助

这是一个CODEPEN来试试

谢谢你们

1 个答案:

答案 0 :(得分:2)

您应该将ng-click="toggle(item.pos);"移至<md-list>

<div class="cnt" ng-repeat="item in data">
    <div class="menu-item">
        <md-list layout="row" layout-padding="" class="layout-row" layout-align="start center" flex ng-click="toggle(item.pos);"> 
            <span class="title flex" flex=""> {{item.name}}</span>
            <i class="fa fa-chevron-down" ng-class="{'rotate180': item.pos==menuIsOpen, 'rotate-back': !menuIsOpen}"></i>
        </md-list>

        <div class="sub-menu" ng-animate="'animate'" >
            <div ng-repeat='(k,v) in item track by $index'>
                <div ng-if="isArray(v)">
                    <md-menu-item ng-if="menuIsOpen===item.pos" ng-repeat='v1 in v track by $index'>                        
                        <md-button>
                            <div layout="row" flex="">
                                <md-switch class="md-primary" name="special" ng-model="project.special" required>

                                <span class="">
                                    <p flex="">{{v1.title}}</p>
                                </span>
                              </md-switch>
                            </div>
                        </md-button>    
                    </md-menu-item>
                </div>
            </div>              
        </div> 
    </div>
</div>