Tip: This guide assumes you've already read the Essentials Guide. Read that first if you're new to Angular.
Every component must have:
- A TypeScript class with behaviors such as handling user input and fetching data from a server
- An HTML template that controls what renders into the DOM
- A CSS selector that defines how the component is used in HTML
You provide Angular-specific information for a component by adding a @Component
decorator on top of the TypeScript class:
@Component({ selector: 'profile-photo', template: `<img src="profile-photo.jpg" alt="Your profile photo">`,})export class ProfilePhoto { }
For full details on writing Angular templates, see the Templates guide.
The object passed to the @Component
decorator is called the component's metadata. This includes the selector
, template
, and other properties described throughout this guide.
Components can optionally include a list of CSS styles that apply to that component's DOM:
@Component({ selector: 'profile-photo', template: `<img src="profile-photo.jpg" alt="Your profile photo">`, styles: `img { border-radius: 50%; }`,})export class ProfilePhoto { }
By default, a component's styles only affect elements defined in that component's template. See Styling Components for details on Angular's approach to styling.
You can alternatively choose to write your template and styles in separate files:
@Component({ selector: 'profile-photo', templateUrl: 'profile-photo.html', styleUrl: 'profile-photo.css',})export class ProfilePhoto { }
This can help separate the concerns of presentation from behavior in your project. You can choose one approach for your entire project, or you decide which to use for each component.
Both templateUrl
and styleUrl
are relative to the directory in which the component resides.
Using components
Every component defines a CSS selector:
@Component({ selector: 'profile-photo', ...})export class ProfilePhoto { }
See Component Selectors for details about which types of selectors Angular supports and guidance on choosing a selector.
You use a component by creating a matching HTML element in the template of other components:
@Component({ selector: 'user-profile', template: ` <profile-photo /> <button>Upload a new profile photo</button>`, ...,})export class UserProfile { }
See Importing and using components for details on how to reference and use other components in your template.
Angular creates an instance of the component for every matching HTML element it encounters. The DOM element that matches a component's selector is referred to as that component's host element. The contents of a component's template are rendered inside its host element.
The DOM rendered by a component, corresponding to that component's template, is called that component's view.
In composing components in this way, you can think of your Angular application as a tree of components.
This tree structure is important to understanding several other Angular concepts, including dependency injection and child queries.