How to get started easily with Syncfusion Angular 7 Menu?
A quick start project that helps you to create an Angular 7 Menu with a minimal code configurations.
Angular 7 Menu
The following section explains you how to create a simple angular 7 Menu component.
Prerequisites
Before start, we need following items to create Angular Menu in Angular 7 application
- Node.js (latest version)
- Angular
- Angular CLI
- Visual studio code for editor.
- Typescript 2.6+
Dependencies
The angular 7 Menu is created from the Syncfusion ej2-angular-navigations package from npmjs, which are distributed in npm as @syncfusion scoped packages.
Creating Angular Project
- Install Angular cli 7 using following command.
npm install -g @angular/cli@7.0.5
If you would like to follow and run the application in Angular6 or Angular5 or Angular4, you need to replace the CLI command version number with the corresponding angular version number.
npm install -g @angular/cli@<CLI VERSION>
- Create a new Angular 7 project using angular cli and navigate to that folder.
ng new <project name> cd <project name>
Adding Angular Menu
- After running the Angular 7 application successfully, configure the Angular Menu in this application. Install Angular Menu and EJ2 package using following command. The --save command will instruct the NPM to include a Menu package inside the dependencies section of the package.json.
npm install @syncfusion/ej2-angular-navigations --save
- Import Menu from installed package.
- Import and inject the other required modules within the providers section of app.module.ts.
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { MenuModule } from '@syncfusion/ej2-angular-navigations'; import { AppComponent } from './app.component'; import { enableRipple } from '@syncfusion/ej2-base'; enableRipple(true); @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, MenuModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule {}
- Add the given below angular menu’s styles in styles.css.
@import "../node_modules/@syncfusion/ej2-base/styles/material.css"; @import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
- Add the angular Menu component in app.component.ts.
import { Component } from '@angular/core'; import { MenuItemModel } from '@syncfusion/ej2-angular-navigations'; @Component({ selector: 'app-root', template: `<ejs-menu [items]='menuItems'></ejs-menu>` }) export class AppComponent { public menuItems: MenuItemModel[] = [ { text: 'File', items: [ {text: 'Open'}, {text: 'Save'}, {text: 'Exit'} ] }, { text: 'Edit', items: [ {text: 'Cut'}, {text: 'Copy'}, {text: 'Paste'} ] }, { text: 'View', items: [ {text: 'Toolbar'}, {text: 'Sidebar'} ] }, { text: 'Tools', items: [ {text: 'Spelling & Grammar'}, {text: 'Customize'}, {text: 'Options'} ] }, {text: 'Go'}, {text: 'Help'} ]; }
- Now serve the application using following command.
ng serve
Once the files are compiled successfully, it will serve the site at localhost:4200
Screenshot:
Group Menu Items with Separator
The separators are both horizontal and vertical lines used to separate the menu items. You can enable separators to group the menu items using the separator property. In the following sample Open and Save sub menu items are grouped using the separator property.
[app.component.ts]
import { Component } from '@angular/core'; import { MenuItemModel } from '@syncfusion/ej2-angular-navigations'; @Component({ selector: 'app-root', template: `<ejs-menu [items]='menuItems'></ejs-menu>` }) export class AppComponent { public menuItems: MenuItemModel[] = [ { text: 'File', items: [ {text: 'Open'}, {text: 'Save'}, {separator: true}, {text: 'Exit'} ] }, { text: 'Edit', items: [ {text: 'Cut'}, {text: 'Copy'}, {text: 'Paste'} ] }, { text: 'View', items: [ {text: 'Toolbar'}, {text: 'Sidebar'}, {text: 'Full Screen'} ] }, { text: 'Tools', items: [ {text: 'Spelling & Grammar'}, {text: 'Customize'}, {text: 'Options'} ] }, {text: 'Go'}, {text: 'Help'} ]; }
Screenshot:
Conclusion
I hope you enjoyed learning about how to get started easily with Syncfusion Angular 7 Menu.
You can refer to our Angular Menu feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Angular Menu example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!