The main job of Angular Components and Templates

Categories: Angular JS Angular

The main job of Angular Components and Templates

 

 Components are building block of Angular application. The main job of Angular Component is to generate a section of web page called view. Every component will have an associated template and it will be used to generate views.

 

Add a component

Let us create a new component in our ExpenseManager application.

Open command prompt and go to ExpenseManager application.

cd /go/to/expense-manager

Create a new component using ng generate component command as specified below:

ng generate component expense-entry

Output

The output is mentioned below:

CREATE src/app/expense-entry/expense-entry.component.html (28 bytes)

CREATE src/app/expense-entry/expense-entry.component.spec.ts (671 bytes)

CREATE src/app/expense-entry/expense-entry.component.ts (296 bytes)

CREATE src/app/expense-entry/expense-entry.component.css (0 bytes)

UPDATE src/app/app.module.ts (431 bytes)

 

Here,

  • ExpenseEntryComponent is created under src/app/expense-entry folder.
  • Component class, Template and stylesheet are created.
  • AppModule is updated with new component.

Add title property to ExpenseEntryComponent (src/app/expense-entry/expenseentry.component.ts) component.

import { Component, OnInit } from '@angular/core';

@Component({

 selector: 'app-expense-entry',

 templateUrl: './expense-entry.component.html',

 styleUrls: ['./expense-entry.component.css']

})

export class ExpenseEntryComponent implements OnInit {

title: string;

 constructor() { }

 ngOnInit() {

 this.title = "Expense Entry"

 }

}

Update template, src/app/expense-entry/expense-entry.component.html with

below content.

<p>{{ title }}</p>

Open src/app/app.component.html and include newly created component.

<h1>{{ title }}</h1>

<app-expense-entry></app-expense-entry>

Here,

app-expense-entry is the selector value and it can be used as regular HTML Tag.

Top articles
Angular JS Interview Question Answer for Freshers and Experienced Published at:- Why we need Angular JS : Benefits of Using AngularJS for Web App Development Published at:- Difference between Angular JS and Angular Published at:- Top Features and Changes Released in Angular 13 Published at:- Angular 13 interview Question and Answer Published at:- The main job of Angular Components and Templates Published at:- Angular MCQ Quiz Question and Answer Published at:- Angular MCQ Quiz Question and Answer Set 2 Published at:- What is data binding in AngularJS Published at:- Some of the advantages of Angular over other frameworks Published at:-
R4Rin Team
The content on R4Rin.com website is created by expert teams.