B
B
beta-it2017-04-14 12:49:00
Angular
beta-it, 2017-04-14 12:49:00

How to hide navbar-collapse on menu item click in Angular 2 in mobile version?

Actually in app.component.html I display the menu (statically):

<nav *ngIf="authService.isLogged()" class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" [routerLink]="['/']">
                <!--<img [src]="imgLOGO" class="img-responsive">-->
            </a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
            <ul class="nav navbar-nav navbar-right">

                <li>
                    <a routerLink="/account1" routerLinkActive="active">Пункт 1</a>
                </li>

                <li>
                    <a routerLink="/login" routerLinkActive="active">Выход</a>
                </li>

            </ul>
        </div><!--/.nav-collapse -->
    </div>
</nav>

And the question is how to hide the expanded menu in the mobile version when clicking on an item?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
beta-it, 2017-04-14
@beta-it

While using this code, after loading the component template:

ngAfterViewInit() {
        $('.navbar-collapse').on('click', 'a', function (e) {

            $('.navbar-collapse').collapse('hide');
        });
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question