Adds the content for lesson 07, a new image, a navigation entry and some updates to the example code for this lesson. Also, this updates previous lessons where typos and copy/pasta errors show up PR Close #49980
3.5 KiB
Lesson 6 - Add a property binding to an component’s template
This tutorial lesson demonstrates how to add property binding to a template and use it to pass dynamic data to components.
Time required: expect to spend about 5 minutes to complete this lesson.
Before you start
This lesson starts with the code from the previous lesson, so you can:
- Use the code that you created in Lesson 5 in your integrated development environment (IDE).
- Start with the code example from the previous lesson. Choose the from Lesson 5 where you can:
- Use the live example in StackBlitz, where the StackBlitz interface is your IDE.
- Use the download example and open it in your IDE.
If you haven't reviewed the introduction, visit the Introduction to Angular tutorial to make sure you have everything you need to complete this lesson.
If you have any trouble during this lesson, you can review the completed code for this lesson, in the for this lesson.
After you finish
- Your app has data bindings in the
HomeComponenttemplate. - Your app sends data from the
HomeComponentto theHousingLocationComponent.
Conceptual preview of Inputs
In lesson 5, you added @Input decorators to properties in the HousingLocationComponent allow the component to receive data. In this lesson, you'll continue the process of sharing data from the parent component to the child component by binding data to those properties in the template. There are several forms of data binding in Angular, in this lesson you'll use property binding.
Property binding enables you to connect a variable to an Input in an Angular template. The data is then dynamically bound to the Input.
For a more in depth explanation, please refer to the Property binding guide.
Lesson steps
Perform these steps on the app code in your IDE.
Step 1 - Update tag in the HomeComponent tempalte
This step adds property binding to the <app-housing-location> tag.
In the code editor:
-
Navigate to
src/app/home/home.component.ts -
In the template property of the
@Componentdecorator, update the code to match the code below:When adding a property binding to a component tag, we use the
[attribute] = "value"syntax to notify Angular that the assigned value should be treated as a property from the component class and not a string value.The value on the right handside is the name of the property from the
HomeComponent.
Step 2 - Confirm the code still works
- Save your changes and confirm the app does not have any errors.
- Correct any errors before you continue to the next step.
Lesson review
In this lesson, you added a new property binding and passed in a reference to a class property. Now, the HousingLocationComponent has access to data that it can use to customize the the component's display.
If you are having any trouble with this lesson, you can review the completed code for it in the .