Learn About Components In Angular

In this article, you will learn in detail about components of Angular. 

  • What is component and uses of Component?
  • What is Template?
  • What is Class?
  • What is Decorator?
  • Default Component Code Explanation
  • Step by Step Creation of New Component

What is Component?

The component is the heart of Angular 2 and above versions. Coding in Angular 2 and all above versions goes through components. It is the basic building block of Angular programming. It is a group of Template, Class, and Decorator.

Angular is written in TypeScript so in Angular we follow the full syntax of TypeScript.

Default component name is app.component.ts .

app.component.ts is root component.

With the help of component, we can distribute or break the logic and reuse specific parts into other components of the component.

What is Template?

A template is a group of HTML, directives, and data binding which helps to render UI (view).

What is Class?

The class is the same as similar classes of C# and Java. Here we follow the syntax of TypeScript. Angular is using the full syntax of TypeScript. The class contains properties, variable and method, and functions.

What is Decorator?

Decorator symbol starts with @. With help of @Decorator, we define metadata components like this:

  1. @Component({  
  2.     selector: 'app-root',  
  3.     templateUrl: './app.component.html',  
  4.     styleUrls: ['./app.component.css']  
  5. })  
Component In Angular

Component Code Explanation

app.component.ts code

  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. import {  
  5.     member  
  6. } from './member';  
  7. @Component({  
  8.     selector: 'app-root',  
  9.     templateUrl: './app.component.html',  
  10.     styleUrls: ['./app.component.css']  
  11. })  
  12. export class AppComponent {  
  13.     title = 'app';  
  14. }  

Component In Angular

LINESR.NO.LINE CODECODE EXPLANATION
1import { Component } from '@angular/core';This line will import the functionality of component from @angular/core library. This is the same as writing USING in C# and IMPORT in JAVA languages.
2import {member} from './member';This line will import the class called “MEMBER”.
3 Blank line
4@Component({Define @decorator of a component. Inside this bracket we define selector, template (HTML) code, and style sheet code.
5selector: 'app-root',Selector means we call or render this component in HTML with this name.  <app-root></app-root>
6templateUrl: './app.component.html'Template URL defines HTML file path where we code our HTML code.
7styles: ['./app.component.css'] StyleUrls define CSS file path where we define CSS code which attached above said HTML file.
8})End of @Component.
9export class AppComponent {Create a class called AppComponent, we had prefixed this with export because in other classes we call this class easily.
10title = 'app';Define title property and assign value “app”
11}End of class

We can divide our component into two parts:

PART 1

Component In Angular

In all components of Angular part 1, we defined our various imports and defined the @component decorator. 

PART 2

Component In Angular

In Part 2 we had defined our class functionality like property and method.

Hope you understand Component very well.


Similar Articles