Buttons. Among other components
Expansion Panels and Accordions
ng add @angular/material
on an existing angular application will add angular material. You will be asked which theme that you want to setup (css reference will be added to angular.json), you'll be asked to setup a global typography set (hammer.js will be imported into main.ts), and you'll be asked to setup browser animations (browseranimationmodule will be added to app.module.ts)
Usually, it's a good idea to keep all of your material components in a separate module file. Create the module with ng g m material
. The following code will import a button component from the material library.
Notice we've removed the reference to CommonModule
that normally appears when you generate a module via the CLI. This is because this module is only meant to be imported later to app.module.ts
. Separating these modules just makes your code more maintainable.
Don't forget to add this module to the imports array of app.module.ts
Similar to the bootstrap classes, you can use classes to style sections of your text. This functionality is available out of the boxes as long as you've added Material via the Getting Started section above. For example, if you were to use
It will render the text to
. You can read more about using different classes to style your document by visiting https://material.angular.io/guide/typography.
Buttons are just one example of a component that you can use with directives imported from the angular material library. To add this and other components to your app, follow the following steps:
app.module.ts
file, add the following import and add it to the imports array of the @NgModule
metadata. Or if you're using the approach of a material module described above in Material Module, add this to the MaterialComponents
array.
This will display the following:
You can read more about buttons by visiting https://material.angular.io/components/button/overview or all components by visiting https://material.angular.io/components/categories.
Note: These buttons and other components use the theme chosen by default but this can be modified with the color
directive.
To find these additional attributes that you can use on the tags, you can refer to the properties for the component
Reading the API for some components will give you the properties available to those components.
To access these properties in HTML, you can use template reference variables by using
The hashtag directive creates a template reference variable that you can use with interpolation in that same template to access the property values. For example, the above code is accessing the color property available on the button.
With some components, this template variable will become very important because you can use it to, for instance, display the chose value(s) of a button group.
Note: notice that we needed to set the value of the variable to matButtonToggleGroup
. This can be seen in the documentation for "exported as"
Will render
Icons are a slightly different component. In addition to importing it to your module as you normally would, the name used in your tag must be the exact name of an icon available within the documentation. Otherwise, the icon will just display the first character of whatever word you specify. For example the below code renders the below image.
Because Gravel is not a recognized icon name. Whereas,
Will render
Because gavel is a recognized name.
You can find a list of all icons by visiting https://material.io/resources/icons/?style=baseline
Some components can take advantage of conditional statements using property binding. For example, in the following HTML, the badge will appear hidden if the number of notification (defined in the component class) is zero.
The sidenav component is also a little different in that you will need to create a container element. Notice also in the below HTML code that the button's click event will simply set opened
equal to the opposite of the opened
value. This is a quick way to toggle the opening and closing of the sidenav
And in the component typescript
Sidenote If the mode attribute for <mat-sidenav>
is set to side may have a more familiar feel. This pushes the content when the sidebar is toggled.
Sidenote You can also use the toggle method rather than setting the opened attribute. This way just requires the use of a template reference variable.
Menus are pretty cool too as you can make use of template variables to define menus and even use <ng-template>
to lazy load in dynamic content
Lists are fairly easy from the documentation but one cool use is for navigation.
For those familiar with the bootstrap grid system, Angular Material also has their own grid system. This grid system works by specifying directives.
Expansion Panels are of particular note because they can be used to save a lot of screen real estate.
You can make this into a set where when one is opened, all others one will be closed. This is referred to as an accordian
Steppers are another component of note because they create a type of wizard for your application. You can make these steps mandatory on the frontend by adding a linear
attribute to the mat-horizontal-stepper
or mat-verical-stepper
tags. You can then add a completed
attribute to each step
Whenever dealing with forms in Angular Material, you must first import the MatFormFieldModule
. For example, if you're planning to use the inputs, you must import both form field and input in @ngModule
It may also be worth mentioning how to get the value from a select group. This is a little different as you'll want to declare a variable in your component class first and then bind that value to the HTML.
Autocomplete can be difficult to implement if you want it to filter the text as you type like in the below gif.
To accomplish this, you'll need to import ReactiveForms in app.module.ts
.
Import the following from rxjs into your component class
Add the following functions to your component class
Finally, use the async pipe in the template file to subscribe to the observable. Don't forget to bind to the form control
DatePicker is another component where you're going to want to import two imports
Snackbars are pretty cool for displaying notifications and it even has some observables tied to actions. In the component class, you will want to import a service MatSnackabar
and inject it into your class.
In the template file, you will simply create a click handler.
Probably the most liked feature of Bootstrap (by me) was modals. Angular Material uses components referred to as Dialogs that act as these and they turn out to be pretty easy to use. They do involve injecting a service which is similar to the snackbars above.
openDialog
that we will define in the next step.DialogComponent
and we'll also import this so that we can use it in the openDialog()
methodLike the snackbar, you are able to create actions when the user closes the dialog by subscribing to an observable. To demo this, we'll edit the dialog template slightly to pass some text on close. We'll simply set the mat-dialog-close
directive to be equal to a string called Hello
In the calling component class, we subscribe to an observable
It's also very useful to be able to specifically pass data to the dialog. All applications of modals in bootstrap that I've used have required me to pass data.
dialog.component.ts
if you're following along), import the inject command from the angular core library and import MAT_DIALOG_DATA
from the material library to get the passed data. We want to pass MAT_DIALOG_DATA
as an injection token to the constructor and use that to define the data property. Notice that data is defined as public so that the dialog can access this.If you have something like a form in your dialog component, it will be useful to be able to send data from the child back to the parent component.
MAT_DIALOG_DATA
also import a DialogRef
DialogRef
Tables are always a very big component in applications. Angular Material has many operations that apply to their version of tables ranging from basic tables to paginated and sorted tables.
ng-container
can use an *ngFor
directive to loop through the columns in a more concise manner.[datasource]
directive to specify the array that will be displayed in the table.mat-hear-row
directive at the bottom uses a *matHeaderRowDef
attribute to define the array of columnmat-row
directive will determine the actual data displayed in the table.Note Notice in the above that you can also use numbers and filter on the weight.
In order the filter the data in tables as a user types, Angular Material provides a special service that can be used on datatables.
$event.target.value
) that will call the applyFilter()
function on every keystroke eventAngular Material uses another module to be able to add sorting to the datatable: MatSortModule.
@ViewChild
decorator. Make sure that this decorator takes MatSort as an argument as well as an object for setting the query resolver to staticngOnInit
lifecycle method. In ngOnInit()
, we will set the sorting property of the data equal to the @ViewChild
variable that we created previously.matSort
directive on the table
element and mat-sort-header
directive on each table header, setting it equal to the column value.Pagination has very similar steps to sorting. Also, like sorting, there is a separate module for this: MatPaginatorModule.
@ViewChild
decorator. Make sure that this decorator takes MatSort as an argument as well as an object for setting the query resolver to staticngOnInit
lifecycle method. In ngOnInit()
, we will set the paginator property of the data equal to the @ViewChild
variable that we created previously.