This tutorial lesson demonstrates how to add interpolation to Angular templates in order to display dynamic data in a template.
What you'll learn
- Your app will display interpolated values in the
HousingLocationComponent
template. - Your app will render a housing location data to the browser.
Conceptual preview of interpolation
In this step you will display values (properties and Input
values) in a template using interpolation.
Using the {{ expression }}
in Angular templates, you can render values from properties, Inputs
and valid JavaScript expressions.
For a more in depth explanation, please refer to the Displaying values with interpolation guide.
-
Update
HousingLocationComponent
template to include interpolated valuesThis step adds new HTML structure and interpolated values in the
HousingLocationComponent
template.In the code editor:
- Navigate to
src/app/housing-location/housing-location.component.ts
- In the template property of the
@Component
decorator, replace the existing HTML markup with the following code:
Update HousingLocationComponent template
import {Component, Input} from '@angular/core';import {CommonModule} from '@angular/common';import {HousingLocation} from '../housinglocation';@Component({ selector: 'app-housing-location', imports: [CommonModule], template: ` <section class="listing"> <img class="listing-photo" [src]="housingLocation.photo" alt="Exterior photo of {{ housingLocation.name }}" crossorigin /> <h2 class="listing-heading">{{ housingLocation.name }}</h2> <p class="listing-location">{{ housingLocation.city }}, {{ housingLocation.state }}</p> </section> `, styleUrls: ['./housing-location.component.css'],})export class HousingLocationComponent { @Input() housingLocation!: HousingLocation;}
In this updated template code you have used property binding to bind the
housingLocation.photo
to thesrc
attribute. Thealt
attribute uses interpolation to give more context to the alt text of the image.You use interpolation to include the values for
name
,city
andstate
of thehousingLocation
property. - Navigate to
-
Confirm the changes render in the browser
- Save all changes.
- Open the browser and confirm that the app renders the photo, city and state sample data.
Summary: In this lesson, you added a new HTML structure and used Angular template syntax to render values in the HousingLocation
template.
Now, you have two important skills:
- passing data to components
- Interpolating values into a template
With these skills, your app can now share data and display dynamic values in the browser. Great work so far.
For more information about the topics covered in this lesson, visit: