> Canonical URL: https://codingcat.dev/course/angularmaterial/lesson/angular-material-router-awareness

# Angular Material Router Awareness | Angular Material | CodingCat.dev Course

[🧶 Angular Material](/course/angularmaterial)Section 1.8 · Lesson 8 of 8[![](/logos/tags/angular-symbol-light.svg)![](/logos/tags/angular-symbol-dark.svg)Angular](/topic/angular)[![](/logos/firebase/firebase-symbol.png)![](/logos/firebase/firebase-symbol.png)Firebase](/topic/firebase)[![](/logos/tags/html-symbol-light.svg)![](/logos/tags/html-symbol-dark.svg)HTML](/topic/html)[![](/logos/tags/github-symbol-light.svg)![](/logos/tags/github-symbol-dark.svg)GitHub](/topic/github)[![](/logos/tags/react-symbol-light.svg)![](/logos/tags/react-symbol-dark.svg)React](/topic/react)[← Prev Lesson](/course/angularmaterial/lesson/angular-material-reactive-forms-update-firestore)

# Angular Material Router Awareness

![](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/a0b269fceac7ec44e3abdc1184a82316c95a8f42-1920x1080.png?w=1920&h=1080&q=82&fit=max&auto=format)[@CodingCatDev62.5K / 100K Subscribers Goal](https://www.youtube.com/@codingcatdev?sub_confirmation=1)[Subscribe on YouTube→](https://www.youtube.com/@codingcatdev?sub_confirmation=1)[Watch on YouTube App ↗](https://www.youtube.com/watch?v=3roNVbp3RPg&list=UUnKZ8gEb78zXKMi1ns-IQ2g)Lesson 8 / 8Course Lessons Outline▾1Section 1
- [1.1Firebase Project Hosting](/course/angularmaterial/lesson/firebase-project-hosting)
- [1.2Angular Material Router Outlet](/course/angularmaterial/lesson/angular-material-router-outlet)
- [1.3Firebase Authentication and Authorization](/course/angularmaterial/lesson/firebase-authentication-and-authorization)
- [1.4Angular Material Theming](/course/angularmaterial/lesson/angular-material-theming)
- [1.5Angular Material Dynamic Navigation using Firestore](/course/angularmaterial/lesson/angular-material-dynamic-navigation-using-firestore)
- [1.6Angular Material Forms from Firestore](/course/angularmaterial/lesson/angular-material-forms-from-firestore)
- [1.7Angular Material Reactive Forms Update Firestore](/course/angularmaterial/lesson/angular-material-reactive-forms-update-firestore)
- [1.8Angular Material Router Awareness](/course/angularmaterial/lesson/angular-material-router-awareness)

## Setup

We can start from the previous lesson and build out our material router. Previous Lesson: [Angular Material Reactive Forms Update Firestore](https://ajonp.com/courses/angularmaterial/angular-material-reactive-forms-update-firestore)

Copied! 🐾

```
git clone
```

This will give us a solid base to start working from, however if you are creating a new firebase project you should change the environment/environment.ts file to match your firebase details. If you have never done this please see [Angular Navigation Firestore](https://ajonp.com/courses/angularmaterial/angular-material-dynamic-navigation-using-firestore) as this will provide more details on how to update.Make sure you update your npm packages

Copied! 🐾

```
npm install
```

## Open/Close Books Drawer

If you are not familiar with Angular [Input and Output Properties](https://angular.io/guide/template-syntax#input-and-output-properties), please do give it a good read so you can understand why we are using the syntax. For the visual learner out there `( )` looks like the letter O in Output and `[ ]` kinda looks like the N for iNput. That is how I try to remember it.

### Template

- `[opened]="showBooksDrawer$ | async"` will take an observable as input with a true/false value, which will open or close the drawer.
- `(activate)="onBooksDrawerActivate($event)"` will output the event observable when the drawer is activated
- `(deactivate)="onBooksDrawerDeactivate($event)"` will output the event observable when the drawer is deactivated

src/app/modules/books/

Copied! 🐾

```

```

### Component

Now that we can pass observables into the `mat-drawer` and receive events out from `router-outlet`, we need to act upon these movements.We can use [RxJS's BehaviorSubject](https://rxjs.dev/api/index/class/BehaviorSubject) to provide an initial value and a stream for observing changes to that value.`showBooksDrawer$ = new BehaviorSubject(false);` will now show a closed drawer upon creation because we are defaulting to a false value./src/app/modules/books/book-drawer/

Copied! 🐾

```
showBooksDrawer$ = new BehaviorSubject(false);
```

The below methods will handle the output that is emmited by the `router-outlet`. You can find more details in the [Angular docs](https://angular.io/api/router/RouterOutlet).

/src/app/modules/books/book-drawer/

Copied! 🐾

```
onBooksDrawerActivate(e) {
    this.showBooksDrawer$.next(true);
}
onBooksDrawerDeactivate(e) {
    this.showBooksDrawer$.next(false);
}
```

## Material Fab Button for book add

If you have never heard of a fab button it stands for [Floating Action Button](https://material.io/design/components/buttons-floating-action-button.html). You can see many different [material examples](https://material.angular.io/components/button/examples) to compare what you think might work best for your situation.Below you will see that we are going to create a pink button (our accent color). So that we can add books to our book list only when we are **verified**  user and in the **correct**  view.

### Adding Required Modules

We need to import both the `MatButtonModule` for the FAB button, as well as the `MatIconModule` for the plus sign.

/src/app/modules/books/

Copied! 🐾

```
import { MatButtonModule, MatIconModule, MatSidenavModule } from '@angular/material';
...
imports: [...MatIconModule, MatButtonModule]
```

### Positioning FAB

We will now need to update the styles so that we can correctly position this new button. Other platforms are good about placing this very specifically and then allowing you to override. If you forget to do this in Angular Material it does not have a default.[/src/app/modules/books/](https://github.com/AJONPLLC/lesson14-angular-material-router-awareness/blob/master/src/app/modules/books/books.component.scss#L19)

Copied! 🐾

```
.app-fab--absolute {
    position: fixed; bottom: 1rem; right: 1rem;
}
@media (min-width: 1024px) {
    .app-fab--absolute {
        bottom: 2rem; right: 2rem;
    }
}
```

### Adding FAB to template

Although this is using a `button` html tag, you then add attributes (technically directives):

- `mat-fab` to provide the rounded shape in our directive.
- `color="accent"` to give this our pink color Check [Material Theming](https://ajonp.com/courses/angularmaterial/angular-material-theming/) for more details.
- `class="app-fab-absolute` is setting up our class that we defined above in our styles
- `routerLink="/books/new"` this is the directive for the Angular router to take action when the button is clicked, see [Angular docs](https://angular.io/api/router/RouterLink)
- `aria-label="Add"` allows for accessiblity features [ARIA](https://developers.google.com/web/fundamentals/accessibility/semantics-aria/)
- `add` is the entire component required for the [add](https://material.angular.io/components/icon/overview) type icon.

/src/app/modules/books/

Copied! 🐾

" aria-label="Copy code" title="Copy code to clipboard">

```

    add

```

## Unsubscribing from Subscriptions

> Please note I have since learned an easier way using a seperate Subject and it is used in the next chapter of this course, here is the source.

If we don't remove subscription from the Observables, we have the chance of memory leaks in our application. So a bit of refactoring our subscriptions allows us to add them all together in an array of type Subscription.

- `subs: Array = [];` array for subscriptions.
- `OnDestroy` you must add the implementation for [OnDestroy](https://angular.io/guide/lifecycle-hooks#ondestroy), so we can remove the subscriptions prior to removing the component (aka directive).

/src/app/modules/books/

Copied! 🐾

```
ngOnDestroy(): void { this.subs.forEach(sub => sub.unsubscribe()); }
```

## Router Events Subscription

In our constructor we can now subscribe to events that happen on the Angular Router instance. We actually only care about the [NavigationEnd](https://angular.io/guide/router#router-events), and also make sure that we are at the `/books` level. At this point we can properly add a true value to the `showBooksAdd$` BehaviorSubject and the UI will react accordingly and update to show the button.

/src/app/modules/books/

Copied! 🐾

```
constructor(private router: Router) {
    /* Only add Book Add Fab on /books */
    this.subs.push(this.router.events.subscribe(e => {
        if (e instanceof NavigationEnd && e.urlAfterRedirects === '/books') {
            this.showBooksAdd$.next(true);
        } else {
            this.showBooksAdd$.next(false);
        }
    }));
}
```

### Hide FAB Button

Now that we are correctly passing the boolean (true/false) value to our BehaviorSubject we can observe this within the Angular UI and react. This is as simple as adding the [NgIf](https://angular.io/api/common/NgIf) directive. The easiest way to accomplish this is by usig the [async pipe](https://angular.io/guide/observables-in-angular#async-pipe) which will automatically subscribe and unsubscribe from our `showBooksAdd$` observable.

- `ngIf="(showBooksAdd$ | async)"` allows for hiding or showing our button determined by the router position.

/src/app/modules/books/

Copied! 🐾

" aria-label="Copy code" title="Copy code to clipboard">

```

    add

```

## Summary

When we are on the `/books` path we want to see the button.

When we are on any other subpath of `/books/*` we want to hide the button, for example the chapter section.

✏️ Visual Architecture Diagram

## how this course connects together.

Follow AJ through the layered stack topology from input contract to production output.

Let’s build it!✏️ Architecture Diagram: Angular Material: Angular Material Router Awaren…LAYERED SYSTEM STACKARCHITECTURE DIAGRAM[Diagram Walkthrough]Content-driven layered-stack diagram for Angular Material: Angular Material Router Awareness: Section 1 -> Angular Material -> Verified Production Artifact.AAA Contrast Verified

## related deep dives.

[View All&rarr;](/blog)[![Sanity Knowledge Bases](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/38b0f021c51d97043c8a1ebf31d505a0ea877b7c-2752x1536.jpg?w=1920&q=82&fit=max&auto=format)](/post/sanity-knowledge-bases)October 17, 2026[![](/logos/tags/mcp-symbol-light.svg)![](/logos/tags/mcp-symbol-dark.svg)MCP](/topic/mcp)[#AI & Agents](/topic/ai)[![](/logos/sanity/Sanity-Symbol--black_on_transparent.svg)![](/logos/sanity/Sanity-Symbol--white_on_transparent.svg)Sanity](/topic/sanity)[+5](/post/sanity-knowledge-bases)

### [Sanity Knowledge Bases](/post/sanity-knowledge-bases)

Eliminate query-time guessing in your AI agents. Learn how Sanity Context Knowledge Bases compile structured datasets, crawled documentation websites, and uploaded files ahead of time, resolve conflicting facts, and serve a compact outline over Context MCP.

[![Alex Patterson](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/a606dfaa381aab08ddbe16a33fe1d3e1fb37c927-400x400.jpg?w=96&h=96&q=82&fit=max&auto=format)Alex Patterson](/author/alex-patterson)[![TanStack Start, Router & Drizzle: End-to-End Type-Safe Full-Stack React](https://cdn.sanity.io/images/hfh83o0w/production/449b767c02cd905aeae6090afd37e8ebba990b7c-1376x768.png?w=1376&q=82&fit=max&auto=format)](/course/tanstack-start-type-safe-full-stack-react)November 4, 2026[![](/logos/tags/react-symbol-light.svg)![](/logos/tags/react-symbol-dark.svg)React](/topic/react)[![](/logos/tags/typescript-symbol-light.svg)![](/logos/tags/typescript-symbol-dark.svg)TypeScript](/topic/typescript)[![](/logos/tags/cloudflare-symbol-light.svg)![](/logos/tags/cloudflare-symbol-dark.svg)Cloudflare](/topic/cloudflare)[+1](/course/tanstack-start-type-safe-full-stack-react)

### [TanStack Start, Router & Drizzle: End-to-End Type-Safe Full-Stack React](/course/tanstack-start-type-safe-full-stack-react)

Build 100% type-safe full-stack React applications with TanStack Start, URL search-param schemas, createServerFn RPCs, Drizzle ORM, and Cloudflare Workers.

[![Alex Patterson](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/a606dfaa381aab08ddbe16a33fe1d3e1fb37c927-400x400.jpg?w=96&h=96&q=82&fit=max&auto=format)Alex Patterson](/author/alex-patterson)[Course Outline](/course/angularmaterial)

## Angular Material

8/81

### Section 1

- [1.1Firebase Project Hosting](/course/angularmaterial/lesson/firebase-project-hosting)
- [1.2Angular Material Router Outlet](/course/angularmaterial/lesson/angular-material-router-outlet)
- [1.3Firebase Authentication and Authorization](/course/angularmaterial/lesson/firebase-authentication-and-authorization)
- [1.4Angular Material Theming](/course/angularmaterial/lesson/angular-material-theming)
- [1.5Angular Material Dynamic Navigation using Firestore](/course/angularmaterial/lesson/angular-material-dynamic-navigation-using-firestore)
- [1.6Angular Material Forms from Firestore](/course/angularmaterial/lesson/angular-material-forms-from-firestore)
- [1.7Angular Material Reactive Forms Update Firestore](/course/angularmaterial/lesson/angular-material-reactive-forms-update-firestore)
- [1.8Angular Material Router Awareness▶](/course/angularmaterial/lesson/angular-material-router-awareness)

@CodingCatDev on YouTube62.5K / 100K

Subscribe for new course lessons and architecture deep dives every week.

[Subscribe for New Lessons →](https://www.youtube.com/@codingcatdev?sub_confirmation=1)