question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MetisMenu Angular 2 - not working on first load

See original GitHub issue

metisMenu not working on initial or first load. After refresh it works fine,

My app.component.ts looks like this,

/// <reference path="../typings/globals/jquery/index.d.ts" />;

import { Router } from '@angular/router';
import { Component, OnInit, AfterViewInit } from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'app-tag',
    templateUrl: 'app.component.html',
})
export class AppComponent implements OnInit, AfterViewInit {
    constructor(private _router: Router) {
    }

    ngOnInit(): void {
    }

    ngAfterViewInit() {
        (<any>$('#side-menu')).metisMenu();
    }
}

My menu items looks like this,

                    <li>
                        <a href="#"><i class="fa fa-volume-control-phone fa-fw"></i> Heading1<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li><a [routerLink]="['/h1c1']">H1-Child1</a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-users fa-fw"></i> Heading 2<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li><a [routerLink]="['/h2c1']">H2-Child1</a></li>
                        </ul>
                    </li>

I’m not getting any error. All the menu items are expanded by default on the first load. When I click on the head item, the page gets refreshed and all the menu items are collapsed and everything works fine from then on…

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

5reactions
robertdelacruz1551commented, Nov 20, 2016

This worked for me

first declare jquery after your imports declare var jQuery:any;

replace this: (<any>$(‘#side-menu’)).metisMenu();

with this: jQuery(‘#side-menu’).metisMenu();

3reactions
ghostcommented, Sep 8, 2017

@BlankHrt - Me neither. In the end I built a vanilla Angular 4 component:

Dependencies:

"@angular/core": "4.1.2",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-beta.4"

Component:

<div class="navbar-default sidebar" role="navigation">
	<div class="sidebar-nav navbar-collapse">
		<ul class="nav" id="side-menu">
			<li *ngFor="let menu of menuDefinition">
				<!-- Top level menu -->
				<a href="{{menu.href}}"
				   *ngIf="!menu.children || menu.children.length == 0">
					<i class="fa {{menu.icon}}"></i>{{menu.text}}
				</a>
				<!-- Top level menu with children -->
				<a href="{{menu.href}}"
				   *ngIf="menu.children && menu.children.length > 0"
				   (click)="menu.expanded=!menu.expanded;false;"
				   [attr.aria-expanded]="menu.expanded"
				   aria-controls="{{menu.childMenuId}}">
					<i class="fa {{menu.icon}}"></i>{{menu.text}}
					<span [ngClass]="{'fa-rotate-270': menu.expanded}" class="fa arrow fa-rotate-270"></span>
					<ul id="{{menu.childMenuId}}"
						[ngbCollapse]="!menu.expanded"
						class="nav nav-second-level">
						<li *ngFor="let childMenu of menu.children">
							<a href="{{childMenu.href}}">{{childMenu.text}}</a>
						</li>
					</ul>
				</a>
			</li>
		</ul>
	</div>
</div>
import {
	Component,
	AfterViewInit
} from '@angular/core';

@Component({
	selector: 'navigation',
	templateUrl: './navigation.component.html',
	styleUrls: ['./navigation.component.css']
})
export class NavigationComponent implements AfterViewInit {

	menuDefinition: Array<any> = [{
		text: 'Dashboard',
		icon: 'fa-dashboard fa-fw',
		href: '#'
	}, {
		text: 'Charts',
		icon: 'fa-bar-chart-o fa-fw',
		href: '#',
		expanded: true,
		childMenuId: 'chartsLinks',
		children: [{
			text: 'Flot Charts',
			href: '#'
		}, {
			text: 'Morris.js Charts',
			href: '#'
		}]
	}, {
		text: 'Tables',
		icon: 'fa-table fa-fw',
		href: '#'
	}, {
		text: 'Forms',
		icon: 'fa-edit fa-fw',
		href: '#'
	}, {
		text: 'UI Elements',
		icon: 'fa-wrench fa-fw',
		href: '#',
		expanded: true,
		childMenuId: 'uiLinks',
		children: [{
			text: 'Panels and Wells',
			href: '#'
		}, {
			text: 'Buttons',
			href: '#'
		}, {
			text: 'Notifications',
			href: '#'
		}, {
			text: 'Typography',
			href: '#'
		}, {
			text: 'Icons',
			href: '#'
		}, {
			text: 'Grid',
			href: '#'
		}]
	}];

	ngAfterViewInit(): void {
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

jquery metismenu not working loading data async with ...
I want to load menu elements from Database and I'm using AngujarJS controller to get the JSON with menu elements. Here is AngularJSController...
Read more >
@metismenu/angular - npm
Start using @metismenu/angular in your project by running `npm i @metismenu/angular`. There are no other projects in the npm registry using ...
Read more >
DevTools failed to load source map - Microsoft Q&A
Dear,. 48h ago, my Sharepoint online apps suddenly stopped working. I see this error in the browser console: DevTools failed to load source...
Read more >
metisMenu - Bountysource
I am using MetisMenu in an angular app, and ran into the problem described ... I have tried the suggestion of first disposing...
Read more >
3 Ways to Solve jQuery - Uncaught ReferenceError: $ is not ...
If you are using jQuery, Angular JS, or plain old JavaScript and getting ... is loaded and getting this error it means your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found