How to declare a variable in a template in Angular?

Image for post
Image for post

1) div + ngIf + let

2) div + ngIf + as

view

component.ts

3) If you don’t want to create a wrapper like div you can use ng-container

view

this will work in most cases but it is not a general solution since it relies on variable being truthy

References:

Are you preparing for interviews? Here are frequently asked interview questions in Angular. It covers the Latest interview questions for Angular and Frontend development. Let’s check how many of these questions you can answer?

Written by

Writing about Angular,JavaScript and Type Script related articles. Please follow, clap stories to motivate us writing more!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store