- Angular 6 - CLI
- Angular 6 - Materials
- Angular 6 - Animations
- Angular 6 - Forms
- Angular 6 - Http Client
- Angular 6 - Http Service
- Angular 6 - Services
- Angular 6 - Routing
- Angular 6 - Pipes
- Angular 6 - Directives
- Angular 6 - Templates
- Angular 6 - Event Binding
- Angular 6 - Data Binding
- Angular 6 - Module
- Angular 6 - Components
- Angular 6 - Project Setup
- Angular 6 - Environment Setup
- Angular 6 - Overview
- Angular 6 - Home
Angular 6 Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Angular 6 - Materials
Materials offer a lot of built-in modules for your project. Features such as autocomplete, datepicker, spder, menus, grids, and toolbar are available for use with materials in Angular 6.
To use materials, we need to import the package. Angular 2 also has all the above features but they are available as part of the @angular/core module. Angular 6 has come up with a separate module @angular/materials.. This helps the user to import the required materials.
To start using materials, you need to install two packages - materials and cdk. Material components depend on the animation module for advanced features, hence you need the animation package for the same, i.e., @angular/animations. The package has already been updated in the previous chapter.
npm install --save @angular/material @angular/cdk
Let us now see the package.json. @angular/material and @angular/cdk are installed.
{ "name": "angular6-app", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "pnt": "ng pnt", "e2e": "ng e2e" }, "private": true, "dependencies": { "@angular/animations": "^6.1.0", "@angular/cdk": "^6.4.7", "@angular/common": "^6.1.0", "@angular/compiler": "^6.1.0", "@angular/core": "^6.1.0", "@angular/forms": "^6.1.0", "@angular/http": "^6.1.0", "@angular/material": "^6.4.7", "@angular/platform-browser": "^6.1.0", "@angular/platform-browser-dynamic": "^6.1.0", "@angular/router": "^6.1.0", "core-js": "^2.5.4", "rxjs": "^6.0.0", "zone.js": "~0.8.26" }, "devDependencies": { "@angular-devkit/build-angular": "~0.7.0", "@angular/cp": "~6.1.3", "@angular/compiler-cp": "^6.1.0", "@angular/language-service": "^6.1.0", "@types/jasmine": "~2.8.6", "@types/jasminewd2": "~2.0.3", "@types/node": "~8.9.4", "codelyzer": "~4.2.1", "jasmine-core": "~2.99.1", "jasmine-spec-reporter": "~4.2.1", "karma": "~1.7.1", "karma-chrome-launcher": "~2.2.0", "karma-coverage-istanbul-reporter": "~2.0.0", "karma-jasmine": "~1.1.1", "karma-jasmine-html-reporter": "^0.2.2", "protractor": "~5.3.0", "ts-node": "~5.0.1", "tspnt": "~5.9.1", "typescript": "~2.7.2" } }
We have highpghted the packages that are installed to work with materials.
We will now import the modules in the parent module − app.module.ts as shown below.
import { BrowserModule } from @angular/platform-browser ; import { NgModule } from @angular/core ; import { BrowserAnimationsModule } from @angular/platform-browser/animations ; import { MatButtonModule, MatMenuModule, MatSidenavModule } from @angular/material ; import { FormsModule } from @angular/forms ; import { AppComponent } from ./app.component ; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, BrowserAnimationsModule, MatButtonModule, MatMenuModule, FormsModule, MatSidenavModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
In the above file, we have imported the following modules from @angular/materials.
import { MatButtonModule, MatMenuModule, MatSidenavModule } from @angular/material ;
And the same is used in the imports array as shown below −
imports: [ BrowserModule, BrowserAnimationsModule, MatButtonModule, MatMenuModule, FormsModule, MatSidenavModule ]
The app.component.ts is as shown below −
import { Component } from @angular/core ; @Component({ selector: app-root , templateUrl: ./app.component.html , styleUrls: [ ./app.component.css ] }) export class AppComponent { myData: Array<any>; constructor() {} }
Let us now add the material-css support in styles.css.
@import "~@angular/material/prebuilt-themes/indigo-pink.css";
Let us now add the material in app.component.html.
<button mat-button [matMenuTriggerFor] = "menu">Menu</button> <mat-menu #menu = "matMenu"> <button mat-menu-item> File </button> <button mat-menu-item> Save As </button> </mat-menu> <mat-sidenav-container class = "example-container"> <mat-sidenav #sidenav class = "example-sidenav"> Angular 6 </mat-sidenav> <span class = "example-sidenav-content"> <button type = "button" mat-button (cpck) = "sidenav.open()"> Open sidenav </button> </span> </mat-sidenav-container>
In the above file, we have added Menu and SideNav.
Menu
To add menu, <mat-menu></mat-menu> is used. The file and Save As items are added to the button under mat-menu. There is a main button added Menu. The reference of the same is given the <mat-menu> by using [matMenuTriggerFor]="menu" and using the menu with # in <mat-menu>.
SideNav
To add sidenav, we need <mat-sidenav-container></mat-sidenav-container>. <mat-sidenav></mat-sidenav> is added as a child to the container. There is another span added, which triggers the sidenav by using (cpck)="sidenav.open()". Following is the display of the menu and the sidenav in the browser −
Upon cpcking opensidenav, it shows the side bar as shown below −
Upon cpcking Menu, you will get two items File and Save As as shown below −
Let us now add a datepicker using materials. To add a datepicker, we need to import the modules required to show the datepicker.
In app.module.ts, we have imported the following module as shown below for datepicker.
import { BrowserModule } from @angular/platform-browser ; import { NgModule } from @angular/core ; import { BrowserAnimationsModule } from @angular/platform-browser/animations ; import { MatDatepickerModule, MatInputModule, MatNativeDateModule } from @angular/material ; import { FormsModule } from @angular/forms ; import { AppComponent } from ./app.component ; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, BrowserAnimationsModule, FormsModule, MatDatepickerModule, MatInputModule, MatNativeDateModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Here, we have imported modules such as MatDatepickerModule, MatInputModule, and MatNativeDateModule.
Now, the app.component.ts is as shown below −
import { Component } from @angular/core ; @Component({ selector: app-root , templateUrl: ./app.component.html , styleUrls: [ ./app.component.css ] }) export class AppComponent { myData: Array<any>; constructor() {} }
The app.component.html is as shown below −
<mat-form-field> <input matInput [matDatepicker] = "picker" placeholder = "Choose a date"> <mat-datepicker-toggle matSuffix [for] = "picker"></mat-datepicker-toggle> <mat-datepicker #picker></mat-datepicker> </mat-form-field>
This is how the datepicker is displayed in the browser.
Advertisements