{ "id": "start", "title": "Getting started with Angular", "contents": "\n\n\n
Welcome to Angular!
\nThis tutorial introduces you to the essentials of Angular by walking you through building an e-commerce site with a catalog, shopping cart, and check-out form.
\nTo help you get started right away, this tutorial uses a ready-made application that you can examine and modify interactively on Stackblitz—without having to set up a local work environment.\nStackBlitz is a browser-based development environment where you can create, save, and share projects using a variety of technologies.
\nTo get the most out of this tutorial you should already have a basic understanding of the following.
\nYou build Angular applications with components.\nComponents define areas of responsibility in the UI that let you reuse sets of UI functionality.
\nA component consists of three things:
\nThis guide demonstrates building an application with the following components.
\n<app-root>
—the first component to load and the container for the other components.<app-top-bar>
—the store name and checkout button.<app-product-list>
—the product list.<app-product-alerts>
—a component that contains the application's alerts.For more information about components, see Introduction to Components.
\n\nTo create the sample project, generate the
In StackBlitz, the preview pane on the right shows the starting state of the example application.\nThe preview features two areas:
\nThe project section on the left shows the source files that make up the application, including the infrastructure and configuration files.
\nWhen you generate the StackBlitz example apps that accompany the tutorials, StackBlitz creates the starter files and mock data for you.\nThe files you use throughout the tutorial are in the src
folder.
For more information on how to use StackBlitz, see the StackBlitz documentation.
\n\nIn this section, you'll update the application to display a list of products.\nYou'll use predefined product data from the products.ts
file and methods from the product-list.component.ts
file.\nThis section guides you through editing the HTML, also known as the template.
In the product-list
folder, open the template file product-list.component.html
.
Add an *ngFor
structural directive on a <div>
, as follows.
With *ngFor
, the <div>
repeats for each product in the list.
Structural directives shape or reshape the DOM's structure, by adding, removing, and manipulating elements.\nFor more information about structural directives, see Structural directives.
\nInside the <div>
, add an <h3>
and {{ product.name }}
.\nThe {{ product.name }}
statement is an example of Angular's interpolation syntax.\nInterpolation {{ }}
lets you render the property value as text.
The preview pane updates to display the name of each product in the list.
\nTo make each product name a link to product details, add the <a>
element around {{ product.name }}
.
Set the title to be the product's name by using the property binding [ ]
syntax, as follows:
In the preview pane, hover over a product name to see the bound name property value, which is the product name plus the word \"details\".\nProperty binding [ ]
lets you use the property value in a template expression.
Add the product descriptions. On a <p>
element, use an *ngIf
directive so that Angular only creates the <p>
element if the current product has a description.
The application now displays the name and description of each product in the list.\nNotice that the final product does not have a description paragraph.\nAngular doesn't create the <p>
element because the product's description property is empty.
Add a button so users can share a product.\nBind the button's click
event to the share()
method in product-list.component.ts
. Event binding uses a set of parentheses, ( )
, around the event, as in the (click)
event on the <button>
element.
Each product now has a Share button.
\nClicking the Share button triggers an alert that states, \"The product has been shared!\".
\nIn editing the template, you have explored some of the most popular features of Angular templates.\nFor more information, see Introduction to components and templates.
\n\nCurrently, the product list displays the name and description of each product.\nThe ProductListComponent
also defines a products
property that contains imported data for each product from the products
array in products.ts
.
The next step is to create a new alert feature that uses product data from the ProductListComponent
.\nThe alert checks the product's price, and, if the price is greater than $700, displays a Notify Me button that lets users sign up for notifications when the product goes on sale.
This section walks you through creating a child component, ProductAlertsComponent
that can receive data from its parent component, ProductListComponent
.
Right click on the app
folder and use the Angular Generator
to generate a new component named product-alerts
.
The generator creates starter files for the three parts of the component:
\nproduct-alerts.component.ts
product-alerts.component.html
product-alerts.component.css
Open product-alerts.component.ts
.\nThe @Component()
decorator indicates that the following class is a component.\n@Component()
also provides metadata about the component, including its selector, templates, and styles.
Key features in the @Component()
are as follows:
selector
, app-product-alerts
, identifies the component.\nBy convention, Angular component selectors begin with the prefix app-
, followed by the component name.@Component()
definition also exports the class, ProductAlertsComponent
, which handles functionality for the component.To set up ProductAlertsComponent
to receive product data, first import Input
from @angular/core
.
In the ProductAlertsComponent
class definition, define a property named product
with an @Input()
decorator.\nThe @Input()
decorator indicates that the property value passes in from the component's parent, ProductListComponent
.
Open product-alerts.component.html
and replace the placeholder paragraph with a Notify Me button that appears if the product price is over $700.
To display ProductAlertsComponent
as a child of ProductListComponent
, add the selector, <app-product-alerts>
to product-list.component.html
.\nPass the current product as input to the component using property binding.
The new product alert component takes a product as input from the product list.\nWith that input, it shows or hides the Notify Me button, based on the price of the product.\nThe Phone XL price is over $700, so the Notify Me button appears on that product.
\nTo make the Notify Me button work, the child component needs to notify and pass the data to the parent component.\nThe ProductAlertsComponent
needs to emit an event when the user clicks Notify Me and the ProductListComponent
needs to respond to the event.
In product-alerts.component.ts
, import Output
and EventEmitter
from @angular/core
.
In the component class, define a property named notify
with an @Output()
decorator and an instance of EventEmitter()
.\nConfiguring ProductAlertsComponent
with an @Output()
allows the ProductAlertsComponent
to emit an event when the value of the notify
property changes.
In new components, the Angular Generator includes an empty constructor()
, the OnInit
interface, and the ngOnInit()
method.\nSince these steps don't use them, the following code example omits them for brevity.
In product-alerts.component.html
, update the Notify Me button with an event binding to call the notify.emit()
method.
Define the behavior that happens when the user clicks the button.\nThe parent, ProductListComponent
—not the ProductAlertsComponent
—acts when the child raises the event.\nIn product-list.component.ts
, define an onNotify()
method, similar to the share()
method.
Update the ProductListComponent
to receive data from the ProductAlertsComponent
.
In product-list.component.html
, bind <app-product-alerts>
to the onNotify()
method of the product list component.\n<app-product-alerts>
is what displays the Notify Me button.
Click the Notify Me button to trigger an alert which reads, \"You will be notified when the product goes on sale\".
\nFor more information on communication between components, see Component Interaction.
\n\nIn this section, you've created an application that iterates through data and features components that communicate with each other.
\nTo continue exploring Angular and developing this application:
\n