Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

li does not disappear on routing to different html page in Angular, Why?

I have a page in that I have a table. There is one column named "Actions".

Action-menu is shown with Edit and Delete, once the user clicks on the action-column in any row. I redirect to another html page once the user clicks on edit.

Everything is working fine. But the menu does not disappear after redirecting to another page. If I go back and again click on a action-column, a new menu with edit and delete appears.

<p-dataTable #dataTable>
    <p-column field="" header="{{l('Actions')}}" [sortable]="false" [style]="{'width':'25px'}">
        <ng-template let-record="rowData" pTemplate="body">
            <div class="btn-group dropdown" normalizePosition>
                <button class="dropdown-toggle btn btn-xs btn-primary blue"
                        data-toggle="dropdown"
                        aria-haspopup="true"
                        aria-expanded="false">
                    <i class="fa fa-cog"></i>
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu">
                    <li>
                        <a (click)="onEditClick(record, $event)">{{ l('Edit') }}</a>
                    </li>
                    <li>
                        <a (click)="onDeleteClick(record, $event)">{{ l('Delete') }}</a>
                    </li>
                </ul>
            </div>
        </ng-template>
    </p-column>
</p-dataTable>

onEditClick(selectedValue: any): void {
   this.router.navigate(['/app/xyz/pqr', selectedClassification.id]);
}
like image 374
vivek nuna Avatar asked Sep 04 '17 11:09

vivek nuna


People also ask

How do you route from one page to another page in angular?

P.S : In order to navigate from one component to another, you must include the RouterModule in corresponding Module Imports array from @angular/router package. You can navigate one to another page in Angular in Two ways. (both are same at wrapper level but the implementation from our side bit diff so.)

What is the difference between routing and navigation in angular?

Angular provides extensive set of navigation feature to accommodate simple scenario to complex scenario. The process of defining navigation element and the corresponding view is called Routing. Angular provides a separate module, RouterModule to set up the navigation in the Angular application.


1 Answers

You are mixing angular with plain bootstrap JavaScript. Every time your DropDown gets clicked, the bootstrap-JavaScript creates the menu somewhere at the bottom of your html markup.

angular-router only updates the markup inside of your router-outlet, so the attached menu markup will not be removed on navigate.

Try to use a angular specific bootstrap version like this:
https://ng-bootstrap.github.io/#/components/dropdown/examples
They should already implemented a solution of your problem, and it will avoid more upcomming issues ;-)

like image 170
Markus Kollers Avatar answered Nov 04 '22 04:11

Markus Kollers