Toggling Templates Dynamically

Learn how to toggle templates dynamically based on roles in Angular.

We have already seen various use cases for attribute directives. In this lesson, we will learn about structural directives that are not so widely used. A typical scenario in enterprise Angular applications is that users should have access to certain application parts according to their role. We may think that we could use the ngIf built-in directive for this. It would be valid for a simple case, but usually, checking a role involves calling some service to get the current user and extracting their role. We could create a more specific structural directive:

  1. Run the following Angular CLI command to create a permission directive:

Note: The command below is for creating a directive on the local machine using the Angular CLI.

Get hands-on with 1400+ tech skills courses.