Icons and Navigation in Angular Context Menu
31 Aug 20266 minutes to read
The ContextMenu component supports two types of contextual enhancements for menu items: configurable icons (CSS classes or image URLs) and click-to-navigate <a> targets. Use these features to give users clear, unambiguous cues for each action.
Icons
The ContextMenu component supports both font-based icons and image-based icons on menu items to provide visual representation of actions and enhance the user experience. To add a font-based icon to a menu item, configure the iconCss property with the appropriate CSS class. By default, icons are positioned to the left side of the menu item text. In the following sample, icons for Cut, Copy, and Paste menu items are added using the iconCss property with built-in e-icons CSS classes.
Font-based icons
Syncfusion ships built-in icon classes in the e-icons font. To apply them to Context Menu items, set the iconCss to the corresponding class name. The e-cm-icons mapping is also provided for common context-menu actions and is automatically registered when the navigations theme stylesheet is imported.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ContextMenuModule } from '@syncfusion/ej2-angular-navigations'
import { enableRipple } from '@syncfusion/ej2-base'
import { Component } from '@angular/core';
import { MenuItemModel } from '@syncfusion/ej2-navigations';
@Component({
imports: [
ContextMenuModule
],
standalone: true,
selector: 'app-root',
template: `<div class="e-section-control">
<div id="target">Right click / Touch hold to open the ContextMenu</div>
<ejs-contextmenu id='contextmenu' target='#target' [items]='menuItems'></ejs-contextmenu>
</div>`
})
export class AppComponent {
public menuItems: MenuItemModel[] = [
{
text: 'Cut',
iconCss: 'e-cm-icons e-cut'
},
{
text: 'Copy',
iconCss: 'e-cm-icons e-copy'
},
{
text: 'Paste',
iconCss: 'e-cm-icons e-paste',
}];
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));URL Navigation
The ContextMenu component enables navigation to external web pages or internal routes when menu items are clicked. This functionality is implemented by configuring the url property with the target destination URL. When a menu item with a URL is selected, the browser navigates to the specified location. In the following sample, navigation URLs for Flipkart, Amazon, and Snapdeal menu items are configured using the url property.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ContextMenuModule } from '@syncfusion/ej2-angular-navigations'
import { enableRipple } from '@syncfusion/ej2-base'
import { Component } from '@angular/core';
import { MenuItemModel, MenuEventArgs } from '@syncfusion/ej2-navigations';
@Component({
imports: [
ContextMenuModule
],
standalone: true,
selector: 'app-root',
template: `<div class="e-section-control">
<div id="target">Right click / Touch hold to open the ContextMenu</div>
<ejs-contextmenu id='contextmenu' target='#target' [items]='menuItems'
(beforeItemRender)='itemBeforeEvent($event)'></ejs-contextmenu>
</div>`,
})
export class AppComponent {
public menuItems: MenuItemModel[] = [
{
text: 'Flipkart',
iconCss: 'e-cart-icon e-link',
url: 'https://www.google.co.in/search?source=hp&q=flipkart'
},
{
text: 'Amazon',
iconCss: 'e-cart-icon e-link',
url: 'https://www.google.co.in/search?q=amazon'
},
{
text: 'Snapdeal',
iconCss: 'e-cart-icon e-link',
url: 'https://www.google.co.in/search?q=snapdeal'
}];
public itemBeforeEvent (args: MenuEventArgs) {
args.element.getElementsByTagName('a')[0].setAttribute('target', '_blank');
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));To open the links in new tab, set
targetattribute with the value_blankin thebeforeItemRenderevent.