Icons and navigation in Angular Context menu component
28 Sep 20245 minutes to read
Icons
The ContextMenu item can have an icon/image in it to provide visual representation of the action. To place the icon on a menu item, set the iconCss
property to e-icons with the required icon CSS. By default, the icon is positioned to the left side of the menu item. In the following sample, the icons for Cut, Copy and Paste menu items are added using the iconCss
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 } 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));
Navigation
Navigation in ContextMenu is usage to navigate to the other web page when menu item is clicked. This can be achieved by providing link to the menu item using the url
property.
In the following sample, Navigation URL for Flipkart, Amazon, and Snapdeal menu items are added 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.