Icons and navigation in Angular Context menu component
4 Sep 20255 minutes to read
Icons
The ContextMenu component supports icons and images on menu items to provide visual representation of actions and enhance user experience. To add an 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 e-icons CSS classes.
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
target
attribute with the value_blank
in thebeforeItemRender
event.