Back to Angular

Add an input parameter to the component

adev/src/content/tutorials/first-app/steps/05-inputs/README.md

22.0.0-next.102.8 KB
Original Source

Add an input parameter to the component

This tutorial lesson demonstrates how to create a component input and use it to pass data to a component for customization.

<docs-video src="https://www.youtube.com/embed/eM3zi_n7lNs?si=WvRGFSkW_7_zDIFD&amp;start=241"/>

NOTE: This video reflects an older syntax, but the main concepts remain valid.

What you'll learn

Your app's HousingLocation template has a HousingLocation property to receive input.

Conceptual preview of Inputs

Inputs allow components to specify data that can be passed to it from a parent component.

In this lesson, you'll define an input property in the HousingLocation component that enables you to customize the data displayed in the component.

Learn more in the Accepting data with input properties and Custom events with outputs guides.

<docs-workflow> <docs-step title="Import the input() function"> In the code editor, import the `input` helper method from `@angular/core` into the `HousingLocation` component. <docs-code header="Import input in housing-location.ts" path="adev/src/content/tutorials/first-app/steps/06-property-binding/src/app/housing-location/housing-location.ts" visibleLines="[1]"/> </docs-step> <docs-step title="Add the Input property"> Add a required property called `housingLocation` and initialize it using `input.required()` with the type `HousingLocationInfo`. <docs-code header="Declare the input property in housing-location.ts" path="adev/src/content/tutorials/first-app/steps/06-property-binding/src/app/housing-location/housing-location.ts" visibleLines="[12]"/>

You have to invoke the required method on input to indicate that the parent component must provide a value. In our example application, we know this value will always be passed in — this is by design. The .required() call ensures that the TypeScript compiler enforces this and treats the property as non-nullable when this component is used in a template.

</docs-step> <docs-step title="Pass data to the input"> Send the `housingLocation` value from the `Home` component to the `housingLocation` property of the HousingLocation component. <docs-code language="angular-ts" header="Declare the input property for HousingLocation in home.ts" path="adev/src/content/tutorials/first-app/steps/06-property-binding/src/app/home/home.ts" visibleLines="[16]"/> </docs-step> </docs-workflow>

SUMMARY: In this lesson, you created a new input property. You also used the .required method to ensure the signal value is always defined.

<docs-pill-row> <docs-pill href="guide/components/inputs" title="Accepting data with input properties"/> <docs-pill href="guide/components/outputs" title="Custom events with outputs"/> </docs-pill-row>