LOTUSMS
LOTUSMS

Reputation: 10240

preventing ng-click event with angular-material

I have a series of toggles inside a custom angular dropdown(s). The problem I am having is, the toggles do toggle, but they close the dropdown as well. How can I prevent this? The dropdowns should still close when double clicking the drawer, that is the expected behavior, but not when the toggle is clicked.

This is the 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>

and the controller has this in it for functionality purposes and data population from json :

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

spoiler alert:adding md-prevent-menu-close="true" to the md-button didn't help

Here is a CODEPEN to try things out

Thanks guys

Upvotes: 0

Views: 318

Answers (1)

Berhthun
Berhthun

Reputation: 116

You should move your ng-click="toggle(item.pos);" into the <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>

Upvotes: 2

Related Questions