-
Run the following commands:
ng new my-app cd my-app ng add @angular/material
ng new my-app
generates the default Angular CLI starter template, you can replacemy-app
with whatever you want to call your appSelect
Y
for routingSelect
SCSS
for stylesng add @angular/material
will install the@angular/cdk
and@angular/material
libraries as well as adding setting up fonts and base stylesIn this example you can choose any of the default themes provided
Select
Y
for animations -
Remove the default Angular App content
Remove everything in
app.component.html
except for<router-outlet></router-outlet>
-
Add in the material module Create folder called
material
yoursrc\app
Copy this file from my github into the folder
-
Add
MaterialModule
to your app importsimport { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { MaterialModule } from './material.module'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, BrowserAnimationsModule, MaterialModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
-
Add some base styles into your app.
Wrap the
<router-outlet></router-outlet>
tag in a div with a classcontainer
The add into yourapp.component.scss
.container { max-width: 800px; margin: 4em auto; padding: 0 20px; } .container>* { display: flex; flex-direction: column; justify-content: center; }
This gives us the basis of a flex layout.
Your app is now ready to use angular material. Set up using the components that work best for you. To continue with an example follow the steps in the next section.
This is example is focused on angular material and does not include the angular code required to fully function
-
Add your first component and set up the route
Run
ng generate component home
-
Set up the routes
Replace the empty routes in
app.routing.module.ts
withconst routes: Routes = [ { path: '', component: HomeComponent }, { path: '**', redirectTo: '/', pathMatch: 'full' } ];
-
Add in the first material components
In this example, I am going to set up a blog and want my home page to have card like previews of each post. Add in the following to
home.html
to start setting this up with angular material<h1>My Blog</h1> <mat-card> <mat-card-header> <mat-card-title>Easy, Breezy, Beautiful</mat-card-title> <mat-card-subtitle>Angular Material</mat-card-subtitle> </mat-card-header> <mat-card-content> <p> This is an amazing blog on angular material. Here is some awesome text about that. Here is some awesome text about that. Here is some awesome text about that. Here is some awesome text about that. Here is some awesome text about that. Here is some awesome text about that. Here is some awesome text about that. Here is some awesome text about that. </p> </mat-card-content> <mat-card-actions> <button mat-button>Read More</button> </mat-card-actions> </mat-card>
-
Run
ng serve
to see your new appWe can already see some of the material design styles but it is still a bit plain. Let's spice things up, follow the next steps and keep
ng serve
running. Check the changes in your browser at each step. -
Add in the following:
- Add
mat-app-background
class to body inindex.html
this changes the background to a color defined in the theme, which defaults to an off-white color - Add
class="mat-display-1"
to the<h1>
on the home page - Change the Read More on the home page into a more defined button by changing
mat-button
tomat-raised-button color="primary"
- Add
align="end"
to themat-card-actions
element to align the buttons to the right of the card - Above the card title, add in an avatar:
<img mat-card-avatar src="https://cdn.pixabay.com/photo/2018/10/11/12/31/black-cat-3739702_1280.jpg" alt="My Photos">
- We want a nice looking header bar, add in
<mat-toolbar class="mat-elevation-z6" color="primary"></mat-toolbar>
outside the container inapp.component.html
We now have a nice home page with a fake preview of a blog post
- Add
-
Now generate a component for the blog content and setup the route
- Run
ng generate component blog
- Add in route:
{ path: 'blog', component: BlogComponent }
- On the Read more button, add
routerLink="/blog"
- Run
-
In
blog.html
add the following:<h1>Easy, Breezy, Beautiful</h1> <h2>Angular Material</h2> <p>Here are some random angular material components</p> <div class="flex-container"> <div> <button mat-raised-button color="primary">Raised Primary</button> <button mat-raised-button color="accent">Raised Accent</button> <button mat-stroked-button>Basic Stroked</button> <button mat-stroked-button color="primary">Stroked Primary</button> </div> <button mat-fab> <mat-icon>thumb_up</mat-icon> </button> <mat-radio-group aria-label="Select an option"> <mat-radio-button value="1">Option 1</mat-radio-button> <mat-radio-button value="2">Option 2</mat-radio-button> </mat-radio-group> <mat-checkbox>Check me!</mat-checkbox> <mat-form-field> <mat-label>Choose a date</mat-label> <input matInput [matDatepicker]="picker"> <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-datepicker #picker></mat-datepicker> </mat-form-field> <mat-slider></mat-slider> <button mat-flat-button color="primary" matTooltip="Info about the action" matTooltipPosition="after" aria-label="Button that displays a tooltip when focused or hovered over"> Tooltip </button> </div> <mat-paginator [length]="100" [pageSize]="10" [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator> <button mat-flat-button color="accent" routerLink="/">Back to home</button>
-
In
blog.scss
add the following.flex-container { display: flex; justify-content: space-around; flex-wrap: wrap; } button { margin: 8px; }
-
Change to a different built in theme
In your
angular.json
change the./node_modules/@angular/material/prebuilt-themes/indigo-pink.css
to./node_modules/@angular/material/prebuilt-themes/pink-bluegrey.css
Take a look at how that changed the components. Angular material has the following built in themes to choose from:- deeppurple-amber.css
- indigo-pink.css
- pink-bluegrey.css
- purple-green.css
These can also be included into your
styles.scss
using@import '@angular/material/prebuilt-themes/pink-bluegrey.css';
// TODO Addd details about theme to the readme
This project was generated with Angular CLI version 9.0.2.
Run ng serve
for a dev server. Navigate to http://localhost:4200/
. The app will automatically reload if you change any of the source files.
Run ng generate component component-name
to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module
.
Run ng build
to build the project. The build artifacts will be stored in the dist/
directory. Use the --prod
flag for a production build.
Run ng test
to execute the unit tests via Karma.
Run ng e2e
to execute the end-to-end tests via Protractor.
To get more help on the Angular CLI use ng help
or go check out the Angular CLI README.