AngularJS ng-click only working on second click

647 Views Asked by At

Attempting to collapse a dropdown menu (language selection), within a mobile bootstrap navbar. On first click, the dropdown menu with the languages opens fine, after changing languages the dropdown language menu closes, but when I attempt to change the language again, I have to double click the dropdown for it to open. Can anyone help me with this?

    <li class="dropdown" dropdown style="margin-top: 25%; left: 7.5%;">
            <a href="#" class="dropdown-toggle" dropdown-toggle role="button" ng-click="visible = true">
                {{vm.languageSel.name}}
                <span class="caret"></span>
            </a >
            <ul class="dropdown-menu" ng-show="visible">
                <li ng-repeat="lang in vm.languages" ng-click="$parent.visible = false">
                    <a ng-click="vm.chgLang($index)">
                        {{lang.name}}
                    </a>
                </li>
            </ul>
        </li>
2

There are 2 best solutions below

1
Maxime Turcotte On

I think it's because visible is initialised by default to false. Try to add ng-init="visible = true" on the first li directive

1
Gobli On

I think it's because of the $parent. Can you try without it? Like this:

<li class="dropdown" dropdown style="margin-top: 25%; left: 7.5%;">
        <a href="#" class="dropdown-toggle" dropdown-toggle role="button" ng-click="visible = true">
            {{vm.languageSel.name}}
            <span class="caret"></span>
        </a >
        <ul class="dropdown-menu" ng-show="visible">
            <li ng-repeat="lang in vm.languages" ng-click="visible = false">
                <a ng-click="vm.chgLang($index)">
                    {{lang.name}}
                </a>
            </li>
        </ul>
    </li>