adev/src/content/tutorials/learn-angular/steps/4-control-flow-if/README.md
@ifDeciding what to display on the screen for a user is a common task in application development. Many times, the decision is made programmatically using conditions.
To express conditional displays in templates, Angular uses the @if template syntax.
NOTE: Learn more about control flow in the essentials guide.
In this activity, you'll learn how to use conditionals in templates.
<hr/>The syntax that enables the conditional display of elements in a template is @if.
Here's an example of how to use the @if syntax in a component:
@Component({
...
template: `
@if (isLoggedIn) {
<p>Welcome back, Friend!</p>
}
`,
})
export class App {
isLoggedIn = true;
}
Two things to take note of:
@ prefix for the if because it is a special type of syntax called Angular template syntaxHere's an example:
template: `
@if (isServerRunning) {
...
} @else {
...
}
`;
Add your code to fill in the missing markup.
</docs-step> </docs-workflow>This type of functionality is called conditional control flow. Next you'll learn how to repeat items in a template.