Introduction to components and templates

From Get docs
Angular/docs/11/guide/architecture-components


Introduction to components and templates

A component controls a patch of screen called a view. For example, individual components define and control each of the following views from the Tour of Heroes tutorial:

  • The app root with the navigation links.
  • The list of heroes.
  • The hero editor.

You define a component's application logic—what it does to support the view—inside a class. The class interacts with the view through an API of properties and methods.

For example, HeroListComponent has a heroes property that holds an array of heroes. Its selectHero() method sets a selectedHero property when the user clicks to choose a hero from that list. The component acquires the heroes from a service, which is a TypeScript parameter property on the constructor. The service is provided to the component through the dependency injection system.

export class HeroListComponent implements OnInit {
  heroes: Hero[];
  selectedHero: Hero;

  constructor(private service: HeroService) { }

  ngOnInit() {
    this.heroes = this.service.getHeroes();
  }

  selectHero(hero: Hero) { this.selectedHero = hero; }
}

Angular creates, updates, and destroys components as the user moves through the application. Your app can take action at each moment in this lifecycle through optional lifecycle hooks, like ngOnInit().

Component metadata

[[../File:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKoAAAAwCAMAAAC2VRGVAAAAmVBMVEX///9Dao91kqw4YYhQdZaBm7OywtFBcZzk6vAfTnlxlbXQ2uOYrsLAzdmhuc6luMkrWIBYgqhcfp2IpsFpiKWMpLp6m7rP2+bC0d+3ytpRfaWnvdH8/f3K2OSTr8fU3+lEc55NeqJki668zdzc5e1KeKH4+vzy9vldhquuwtWBob7s8fZqkLKNqsSZs8vn7fPY4uuzxtidtsz/+VHRAAAG5ElEQVRo3u1ZZ5eqOhSVmoD0JkWKggXs8/9/3EujKc44vpl734eXtVxIQpKdU/Y5SWaz/wsp27w2XLdRVbXJknIB+5blZVEmGW4JXDczbtXy99Esq0UaRTcDlSiF277hUp/W4K6sggTDXRjq5r5pc8zKy+/BvCbB/h7MOjCuqCkN8FuxPhm3KFwsFmH94ark43Vm44d9JIJe4IUmRkPHOd62vyHN1F2R4W2swhMSKsJiF6QqbhDQIqjz+4kPZYO/sLPwcD/eLk2aGHW9vY3okkZG5qptaT7KfIeB1lg0+1NU3aE55KWLm1bJbmq4XQw2Q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X4DYqJdn41yvCPqACFi7uBLA0+fjf+bHiH5eStOJlvkPAwqiQwa6iDq4wkQSEJqWvvlqETFMGBdXbqdQXt6VNyRP3fM3mcYw3jbuzE26jnOliSzX4QuzLuhMDQTGzTrI3XEJONVil5v3+OAdR7RQYF2zMGaArbvBoK9vjVxaGSEu7VB/gwJT5h+p1+W8ThthU6iAssCdK9TCJKAh+0VjqW6DVGsWt3Gm0RwJq4F8QaaGgY3lYjOuR4qdCSmNrQYH4PWqU2bRMEQDSBZFLVHyMGZmzR+8R7sQhOEPkSgoUhEDQVzkOm5aDtafIx8/rB53BXMhUkG84hUsMdoltPGIk7hfPyHxX5dkajdWAwpoqk5iXeMi33GnlC0RJRpIYYRFcoBM4uIYVvV5IgGrOvDfT4dPG4HniSTPCRMrShWp/n5nE4D2xqTvvP+fLw30Vt+77pCnZm6xp66PlP39JRwoxpwIvU/fnPnJ0smnP1SwXnf+nSbPq/Y7sHp7+/7CceDY7m4LD/f+6/D/wBSGIP6yzNdZMS2Uf1lpNEKNC8c2tcxOf/+KK+XywW5XV4mZ/hHgaYvn60u09PwsBxHg/LH4QTHJ8lxZaDJV9E3zjbD5KSqe9tekTPL648LDmzCB1Vt85oIyU52b/riGh/Zf3WCanzrIC6P6VWK656NJIoiwzg3R7qpj0//5uwRg0VoP7uhCcE3TriW9QY0U0f2a7f+14fK5IQTHxAk5fVRObsqypDi1NcEu4z2bF0HcvtzxqI16jD/sbuq3Aji9kJKDcidD5rEDY72QCr1l+f2VYbvOdJfZxHY31WMru/2gRFWO3IrdDTSpz5xiUjqsS7/GO3t8kVI7iexQ6T5pWPAJUnWEJbmo6wuw5vLKq0zqpQ4+9vhpeObtL80LWxaukvUTZDky9l/qSwrTMr7uLcQe6+ekvDnrqT/AaPYg1JXYyxEAAAAAElFTkSuQmCC|170x48px|class=left|Metadata]]

The @Component decorator identifies the class immediately below it as a component class, and specifies its metadata. In the example code below, you can see that HeroListComponent is just a class, with no special Angular notation or syntax at all. It's not a component until you mark it as one with the @Component decorator.

The metadata for a component tells Angular where to get the major building blocks that it needs to create and present the component and its view. In particular, it associates a template with the component, either directly with inline code, or by reference. Together, the component and its template describe a view.

In addition to containing or pointing to the template, the @Component metadata configures, for example, how the component can be referenced in HTML and what services it requires.

Here's an example of basic metadata for HeroListComponent.

@Component({
  selector:    'app-hero-list',
  templateUrl: './hero-list.component.html',
  providers:  [ HeroService ]
})
export class HeroListComponent implements OnInit {
/* . . . */
}

This example shows some of the most useful @Component configuration options:

  • selector: A CSS selector that tells Angular to create and insert an instance of this component wherever it finds the corresponding tag in template HTML. For example, if an app's HTML contains <app-hero-list></app-hero-list>, then Angular inserts an instance of the HeroListComponent view between those tags.
  • templateUrl: The module-relative address of this component's HTML template. Alternatively, you can provide the HTML template inline, as the value of the template property. This template defines the component's host view.
  • providers: An array of providers for services that the component requires. In the example, this tells Angular how to provide the HeroService instance that the component's constructor uses to get the list of heroes to display.

Templates and views

[[../File:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAAByCAMAAAAh126/AAAACXBIWXMAAAsTAAALEwEAmpwYAAADAFBMVEVHcExvb283NjZKSkqDg4Oenp5QUFARDw9TU1Pa2trm5ub29vb39/cPCQnYOy/6RTejLSQ5EAzgPTGrLybXOy/V1dXROS6mLiUdEA9lGxbyQjXKysrr6un////rQDTFNit3IBpAJSTm5ub///+IiIh4eHju7u6RkZHz8/Pu7u7x8fEaI37v7+/r6+v5+fns7Ozx8fDl5eXt7e3m5ubg4ODc3Nz39/coNZO1tbXk5OT09PTU1NTe3t7h4eG8vL3q6urd3d2ysrLNzc3CwsLp6enb29sPnVj/wQfo6Ojf39/n5+cGmlLi4uLk4+PLy8spNpTz8/K4uLjExMS0tLTZ2dgmM48SHHoiHh+6urocJ3oZInj29vYmM5IYIXwYIHT/wACzs7P/vwDt8Pf28/QjLoP39vP1Qzbyz2MXH3EVHW0fKYDz8/CLyqv/uk4WH3ze4OYiLoja2tolMYsaJXIcGRvp6efX19fJyckXIG7Q0M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bPw+aq1fWRBsv1icd9HH2Yrz6dPClE4nmPEdTY2XnVBnh6sTz/evaJJRnX6JS/erK299jvDMF3dOjnZNf92UkYVJP45+/FZifoWic3d+dmSjcYSJKK6lNjuKkiYLK3wjIxw13o4RGfwC7JcbrXq9Xqr1SqXe/U6GeXBftysVCrNSrOJZVfN32etXxdP+nXRHt3pF7b9e0uDOj4tt+cG9W0qCEO8eqc5YD7o3vEdkUQzuYE8yES6t3i/X4vHpx/ac/f6NfclRNMjDOIIrRBCWEpIBBcoJLDZnrqRF8gMjYOhIEfDQWCRhHkLGWCTEsWKkSBNmSPe90jjMDeQUKTRMJDzo/pQkACWeIKGTSQAIKOKZgDAMmYpZYoFuYEQbtXe0mK/ls6xRYaMEUDAo03OUktTin4HAGRCpALq3LH8QKwzXx8N6s+2+7EJqm5WQZhBvbr53VnGueVKGatE5oxzwjmopU5SW9D2Vl4gmVF2eXl7GdPLXkIZqW2tUavVVKOBZcOnhuPaammNlMpyQ9Hg4fCqQq9HcSt5biApxxjQTIKgClInkHSSeNNGS7RnLOTSWI1fFxs0oLRWRhcSqUwCT7xVJdGhKmhdyA+EcYhVGu1DQ+MJ40GOBAp0mXwmPZbUxjtrwOo9Nozd+1OJfwPxZW4gIRM9YW/3JR62dIKyyydYc4L1vs0VGe3dvyydg9sCU5bXYL99/T/rzs1rE0000UQT5aq/V9uvTw/Xrn8AAAAASUVORK5CYII=|200x114px|class=left|Template]]

You define a component's view with its companion template. A template is a form of HTML that tells Angular how to render the component.

Views are typically arranged hierarchically, allowing you to modify or show and hide entire UI sections or pages as a unit. The template immediately associated with a component defines that component's host view. The component can also define a view hierarchy, which contains embedded views, hosted by other components.

A view hierarchy can include views from components in the same NgModule, but it also can (and often does) include views from components that are defined in different NgModules.

Template syntax

A template looks like regular HTML, except that it also contains Angular template syntax, which alters the HTML based on your app's logic and the state of app and DOM data. Your template can use data binding to coordinate the app and DOM data, pipes to transform data before it is displayed, and directives to apply app logic to what gets displayed.

For example, here is a template for the Tutorial's HeroListComponent.

<h2>Hero List</h2>

<p><i>Select a hero from the list to see details.</i></p>
<ul>
  <li *ngFor="let hero of heroes" (click)="selectHero(hero)">
    {{hero.name}}
  </li>
</ul>

<app-hero-detail *ngIf="selectedHero" [hero]="selectedHero"></app-hero-detail>

This template uses typical HTML elements like <h2> and <p>, and also includes Angular template-syntax elements, *ngFor, Template:Hero.name, (click), [hero], and <app-hero-detail>. The template-syntax elements tell Angular how to render the HTML to the screen, using program logic and data.

  • The *ngFor directive tells Angular to iterate over a list.
  • Template:Hero.name, (click), and [hero] bind program data to and from the DOM, responding to user input. See more about data binding below.
  • The <app-hero-detail> tag in the example is an element that represents a new component, HeroDetailComponent. HeroDetailComponent (code not shown) defines the hero-detail child view of HeroListComponent. Notice how custom components like this mix seamlessly with native HTML in the same layouts.

Data binding

Without a framework, you would be responsible for pushing data values into the HTML controls and turning user responses into actions and value updates. Writing such push and pull logic by hand is tedious, error-prone, and a nightmare to read, as any experienced front-end JavaScript programmer can attest.

Angular supports two-way data binding, a mechanism for coordinating the parts of a template with the parts of a component. Add binding markup to the template HTML to tell Angular how to connect both sides.

The following diagram shows the four forms of data binding markup. Each form has a direction: to the DOM, from the DOM, or both.

This example from the HeroListComponent template uses three of these forms.

<li>{{hero.name}}</li>
<app-hero-detail [hero]="selectedHero"></app-hero-detail>
<li (click)="selectHero(hero)"></li>
  • The Template:Hero.name interpolation displays the component's hero.name property value within the <li> element.
  • The [hero] property binding passes the value of selectedHero from the parent HeroListComponent to the hero property of the child HeroDetailComponent.
  • The (click) event binding calls the component's selectHero method when the user clicks a hero's name.

Two-way data binding (used mainly in template-driven forms) combines property and event binding in a single notation. Here's an example from the HeroDetailComponent template that uses two-way data binding with the ngModel directive.

<input type="text" id="hero-name" [(ngModel)]="hero.name">

In two-way binding, a data property value flows to the input box from the component as with property binding. The user's changes also flow back to the component, resetting the property to the latest value, as with event binding.

Angular processes all data bindings once for each JavaScript event cycle, from the root of the application component tree through all child components.

Data binding plays an important role in communication between a template and its component, and is also important for communication between parent and child components.

Pipes

Angular pipes let you declare display-value transformations in your template HTML. A class with the @Pipe decorator defines a function that transforms input values to output values for display in a view.

Angular defines various pipes, such as the date pipe and currency pipe; for a complete list, see the Pipes API list. You can also define new pipes.

To specify a value transformation in an HTML template, use the pipe operator (|).

Template:Interpolated value

You can chain pipes, sending the output of one pipe function to be transformed by another pipe function. A pipe can also take arguments that control how it performs its transformation. For example, you can pass the desired format to the date pipe.

<!-- Default format: output 'Jun 15, 2015'-->
  <p>Today is {{today | date}}</p>

 <!-- fullDate format: output 'Monday, June 15, 2015'-->
 <p>The date is {{today | date:'fullDate'}}</p>

  <!-- shortTime format: output '9:43 AM'-->
  <p>The time is {{today | date:'shortTime'}}</p>

Directives

[[../File:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACTCAMAAABbEhBjAAAACXBIWXMAAAsTAAALEwEAmpwYAAABs1BMVEX////5+vzG1eL8/f3s8fX6/P1KeKH+/v7+//9Pe6OXscnu8vb2+Pr4+ftBcZzy9fhHdZ/l6/FbhKrf5+7p7vPr8PTn7fJRfaVEc57Y4utWgadwlLXi6fB0mLfd5e3U3+nk6vDw9PZMeaI/cJvh6O9mjbA3apeCor6Iobiov9L09vl4mrn19/rDz9uFnrZ/n7yessWwxNa3ytp8nbtYgqjX4epfh6xQdZbO2ePc4+rE0+HG0t2huc5pj7Fiiq45a5jK1d+IpsFdhquuwtWOq8RtkrOFpMDw9PjU3eWitcfR2+RUf6bAzdqBm7NLcZNti6ibtctzkKuet82MpLqOprs8bpq6ydaxwdCTqr99mLF2k62lvNC7zNyRqL2LqMNZe5y4xtREa4+9y9hkhaOzx9hgiKy+z96Ur8iWrMBnh6RwjqqbsMQ6Y4nX3+czZpXP3OaRrcasvc2zw9JVeJnB0d+ZrsLM2OLS3eh6lq8nVH4/Z4xki64yXYWvv89qiaY2YIfJ1+NhgqFdf56nuctfgJ8sYZGswdQsWIGluMlHbpGqvMxcfp0eTngvZJNLdZotY5J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Pkd3wDc3mtmSxpmfGVRnanaKyDmhZr1QU2+2QvyKOL214xOC2BvJE67ZrVNTQM1Jo7R+7qbT/RoNXzJqOO2cDyDTCHK57TkZYteGmkXzAG2dSeP/8cLFAO6TInpGWRFI0lRvVgPXVtnmQaLCVKlwuSlvlv6CTpz4xTC4LXJgfzSRxPB6RKHD2jk482sUtNq2e8F6yx2QKpOeXda8tNiwmaY6PMaMmKdjI01mNgtJSLU2CNiU2HZK1d5mfLTQs4x4Z1eeE1ntjdP5mCWYdyyavllZvWD+AcG1Y8kbu4yH/uL5XpIpNL0TssA0Krv74fuD3e2rZ/+fOyUn2WfHTQO41WbZBS2y1gpxkHh/ICJfreXGrKSls1VVBlwZy2UXf6jKS1wrQUGy3UI6TXJawwrT3JBLXxnR5iVpjWZjtAfKviAXBo5b8n1JWGJjmVZcValQJ42GOV7kQRmkQNMBJPK0vrtH4CYKoaZQ9UlBVjpRMkujBSN1dRWtwxoG3OpsXBitJq7VwFCRzyXWOC/ysP3nBrM7fEBDZIQfoQggcbm/+esmBgwRj4GHgo+noEAkHrqBz9Bp6UbTPMDVwPn/54WwTKN4AYc6UkQj7STulDeEws9xzLP9cQECoJDGPJ5iPr/ShG5xDMT02gMLSgkG+gmKxhs+s662UdM1Mz8vNGV5u0brFdWmMAW1FW4587+EN5/X0K/dKcvOMcxzeXBIOrpp3A+F9/4sCPco7XYUBPKre1SbCU/kaMkG0MxfRbS0vvRD9hOpmtm93z+7XdoGFtqHmxY3R0f3Coc7dtw2bb6JATaDEQ6JrCsbo/R2C+G8V/BjCMYJLbO0XRrFEoFJoGLgoBi/F3TnnCK+zdv+qGekePTjUKjaZBVPycIY/AP3QkiSUcblU2qKF/8Rn/A9dEdMpwf3YnAAAAAElFTkSuQmCC|150x147px|class=left|Directives]]

Angular templates are dynamic. When Angular renders them, it transforms the DOM according to the instructions given by directives. A directive is a class with a @Directive() decorator.

A component is technically a directive. However, components are so distinctive and central to Angular applications that Angular defines the @Component() decorator, which extends the @Directive() decorator with template-oriented features.

In addition to components, there are two other kinds of directives: structural and attribute. Angular defines a number of directives of both kinds, and you can define your own using the @Directive() decorator.

Just as for components, the metadata for a directive associates the decorated class with a selector element that you use to insert it into HTML. In templates, directives typically appear within an element tag as attributes, either by name or as the target of an assignment or a binding.

Structural directives

Structural directives alter layout by adding, removing, and replacing elements in the DOM. The example template uses two built-in structural directives to add application logic to how the view is rendered.

<li *ngFor="let hero of heroes"></li>
<app-hero-detail *ngIf="selectedHero"></app-hero-detail>
  • *ngFor is an iterative; it tells Angular to stamp out one <li> per hero in the heroes list.
  • *ngIf is a conditional; it includes the HeroDetail component only if a selected hero exists.

Attribute directives

Attribute directives alter the appearance or behavior of an existing element. In templates they look like regular HTML attributes, hence the name.

The ngModel directive, which implements two-way data binding, is an example of an attribute directive. ngModel modifies the behavior of an existing element (typically <input>) by setting its display value property and responding to change events.

<input type="text" id="hero-name" [(ngModel)]="hero.name">

Angular has more pre-defined directives that either alter the layout structure (for example, ngSwitch) or modify aspects of DOM elements and components (for example, ngStyle and ngClass).

Learn more in the Attribute Directives and Structural Directives guides.

© 2010–2021 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v11.angular.io/guide/architecture-components