#07 What is a Component | Angular Components & Directives| A Complete Angular Course

procademy
29 May 202311:54

Summary

TLDRThis lecture provides an introduction to components in Angular, a key feature of the framework for building client-side applications. It explains that an Angular application is built by combining multiple independent components, starting with a root component (often called the app component). The lecture also covers the parent-child relationship between components and demonstrates how to create a component in Angular. Key steps include creating a TypeScript class, decorating it with the @Component decorator, and declaring it in the module file. A simple example of building a header component is shown.

Takeaways

  • πŸ“¦ Angular is a component-based JavaScript framework used for building client-side applications.
  • 🌳 Every Angular application has at least one root component, conventionally named 'App Component', which can contain child components.
  • 🧩 A component is a small, reusable piece of a user interface, and Angular applications are built by combining multiple components.
  • πŸ“ Components can be organized independently, such as a header, sidebar, footer, or content area, and then combined to form the overall application interface.
  • πŸ”— Components can also have nested child components, forming a parent-child relationship, like a navbar component inside a header component.
  • πŸ—οΈ To create a component in Angular, follow three steps: create a TypeScript class, decorate it with the @Component decorator, and declare it in the main module file.
  • πŸ“‘ The @Component decorator is used to define a component, including its selector and template, which specify how the component will render in the DOM.
  • πŸ—‚οΈ Angular components must be registered in the 'declarations' array of the Angular module to be recognized and used by the framework.
  • πŸ”€ Components can be inserted into the root component or other components using their selector (e.g., 'app-header' for a header component).
  • πŸ› οΈ The template for a component can be directly defined using the 'template' property or by specifying an external HTML file using 'templateUrl'.

Q & A

  • What is a component in Angular?

    -A component in Angular is a small, reusable piece of a user interface. Components are the building blocks of Angular applications, and each Angular app has at least one component called the root component.

  • What is the purpose of the root component in an Angular application?

    -The root component, often referred to as the 'app component', represents the entire Angular application. It is the parent component that contains all other components and forms the structure of the app.

  • How can different parts of the user interface be structured in Angular?

    -In Angular, the user interface is split into separate components. For example, a web application could have components for the header, navbar, sidebar, main content, and footer. Each of these can be built independently and combined to create the full user interface.

  • What is the convention for naming a root component in Angular?

    -By convention, the root component of an Angular application is named 'app component', although it can be named anything.

  • How do components form a parent-child relationship in Angular?

    -In Angular, components can have a parent-child relationship where the root component (e.g., 'app component') contains child components like the header, navbar, and footer. Similarly, each child component can have its own child components.

  • What are the three main steps to creating and using a component in Angular?

    -The three steps to create and use a component in Angular are: 1) Create a TypeScript class and export it, 2) Decorate the class with the '@Component' decorator, and 3) Declare the component in the main module file.

  • What is the purpose of the '@Component' decorator in Angular?

    -The '@Component' decorator in Angular is used to define a class as a component. It provides metadata, such as the selector and template, that Angular uses to understand how the component should be rendered.

  • What is a selector in Angular, and how is it used?

    -A selector in Angular is a property defined in the '@Component' decorator. It represents the custom HTML tag that will be used to display the component in the template. For example, the selector 'app-header' can be used in HTML as '<app-header></app-header>'.

  • How can the template of a component be defined?

    -The template of a component in Angular can be defined using either the 'template' property (inline HTML within the component) or the 'templateUrl' property (linking to an external HTML file).

  • How does Angular become aware of a new component?

    -Angular becomes aware of a new component when it is declared in the 'declarations' array of the module (typically 'AppModule'). The component also needs to be imported in the module file.

Outlines

00:00

πŸ“˜ Introduction to Angular Components

In this paragraph, the concept of components in Angular is introduced. Components are described as a key feature of Angular, a JavaScript framework used to build client-side applications. The paragraph explains that Angular applications are made up of multiple independent components, which are then combined to form a complex user interface. Every Angular application has at least one root component, typically called the 'app component', which houses other child components. The paragraph emphasizes that Angular applications are essentially structured as a tree of components.

05:02

πŸ” Breaking Down UI into Components

This paragraph discusses how Angular divides a user interface (UI) into independent components. A simple web application is used as an example, where different parts like the header, navbar, sidebar, footer, and main content area can be built as separate components. These components can also have their own sub-components, which are inserted into their parent components. This modular approach allows developers to work on each UI section independently before combining them into a full application. The app component is highlighted as the root component that ties everything together.

10:02

πŸ› οΈ Steps to Create an Angular Component

The paragraph outlines the process of creating a new component in Angular. It begins by explaining that a component is essentially a TypeScript class. The steps include creating a class, decorating it with the `@Component` decorator, and specifying key properties like the selector and template. The example walks through creating a 'header' component by first defining its folder structure inside the app folder. It also explains how to name the component and organize the TypeScript file properly, setting up the component to be reused in the application.

πŸ“‚ Setting Up and Exporting Component Class

Here, the detailed process of creating a TypeScript class for a component is explained. After creating a file for the 'header' component, the class is defined with the 'class' keyword and exported for use in other files. To convert this TypeScript class into a component, the `@Component` decorator is added. This decorator requires importing from Angular's core library and includes important metadata such as the selector and template. The paragraph covers the basic syntax needed to set up the component class and includes an HTML template as part of the view.

πŸ–₯️ Declaring Components in the Angular Module

This paragraph focuses on how to make Angular aware of newly created components by declaring them in the module file. The example uses the 'header' component, which needs to be imported into the app module. Once imported, the component must be added to the 'declarations' array so that Angular knows about it and can use it within the application. The process of registering a component within a module is essential for making it functional in an Angular project.

πŸ”— Using Component Selector in HTML

The paragraph explains how to use a component in the application by referencing its selector in the HTML. For the 'header' component, the selector 'app-header' is used to embed it into the root component's HTML. When this selector is added, the content from the header component’s template, such as an `h3` element displaying the app name ('Ecart'), will appear on the web page. This demonstrates how components interact with the DOM using their defined selectors.

βœ”οΈ Component Creation Recap and Error Handling

This section recaps the entire process of creating and using components in Angular. It reiterates the steps: defining a TypeScript class, using the `@Component` decorator, specifying a selector and view template, and registering the component in the module. The paragraph also highlights the importance of having a view template, as missing it can cause errors. Finally, it emphasizes the need to declare components in the module file to ensure they are recognized and usable in the Angular application.

Mindmap

Keywords

πŸ’‘Component

A component is a key building block in Angular applications. It is a small, reusable piece of the user interface, each with its own functionality and structure. In the video, components are described as the fundamental units used to build complex client-side applications by splitting the UI into separate, independent parts, like the header, footer, and sidebar.

πŸ’‘Root Component

The root component is the main component in an Angular application, from which all other components are structured. Typically called the 'app component,' this serves as the parent for all other child components, forming a component tree. The video explains how the root component represents the entire Angular application.

πŸ’‘Child Component

A child component is nested within another component, known as its parent. Child components inherit functionality and interact with their parent components. For instance, the video mentions that the header, sidebar, and footer are child components of the root app component, showing how components are organized hierarchically.

πŸ’‘Selector

A selector is an identifier for a component in Angular, used to place the component within the HTML structure. It is a property within the component decorator. In the video, the selector for the header component is 'app-header,' which is later used in the app component's HTML to insert the header component.

πŸ’‘Decorator

A decorator is a special syntax in TypeScript used to attach metadata to classes, methods, and properties. In Angular, the '@Component' decorator is used to turn a simple TypeScript class into an Angular component. The video explains how this decorator is applied to the TypeScript class of a component to enable its functionality within Angular.

πŸ’‘Template

A template in Angular defines the view layer of a component, representing the HTML structure that the component will render in the browser. In the video, the header component uses a simple template containing an h3 element. This template determines how the content of the header component appears on the webpage.

πŸ’‘Module

A module in Angular is a container for different parts of an application, including components, services, and other modules. The video mentions the 'app module' as the main module where components are declared and registered, ensuring Angular knows about them and how they are structured in the application.

πŸ’‘Declaration

Declarations in Angular refer to the process of registering components, directives, and pipes within a module. Without proper declaration, Angular will not recognize a component. The video explains how to declare the header component within the app module, making it available for use in the root component.

πŸ’‘Client-Side Application

A client-side application is software that runs entirely within the user's browser, handling interactions and rendering without the need to reload the entire page. The video highlights that Angular is a framework specifically designed for building client-side applications, which are composed of numerous components working together.

πŸ’‘Parent-Child Relationship

In Angular, the parent-child relationship defines how components are structured and interact with one another. A parent component can pass data to its child components, while child components can send events back to the parent. The video illustrates this with examples, such as how the app component is the parent of the header, sidebar, and footer components.

Highlights

Angular is a component-based JavaScript framework for building client-side applications.

A component is essentially a small piece of the user interface.

Every Angular application has a root component, commonly referred to as the app component.

Angular applications are structured as a tree of components, which come together to form the user interface.

Developers can split sections of the web application into independent components (e.g., header, footer, sidebar) to work on them separately.

Each component can have child components, like a navbar inside a header component, or individual menu items within a navbar component.

Parent-child relationships exist between components, where the parent can include and control multiple child components.

To create a component, you first need to create a TypeScript class and export it.

You must use the @Component decorator to transform a TypeScript class into a component.

In Angular, components must have a view template, which can be defined inline using the template property or through an external file with templateUrl.

The selector property in a component allows it to be used as a custom HTML tag.

Once a component is created, it must be declared in the module's declarations array for Angular to recognize it.

The root component file (app.component.html) is where the other components, like header or footer, are inserted using their selectors.

After importing and declaring a component, you can use its selector as an HTML element in the template to render its content.

Without a view template, Angular throws an error indicating that the component is missing essential information.

Transcripts

play00:00

hello and welcome to another lecture of

play00:02

this complete angular course in this

play00:04

lecture we are going to learn about

play00:05

components in angular a component is the

play00:08

key feature of an angular application

play00:12

so angular is a component based

play00:15

JavaScript framework for building

play00:16

client-side applications we have learned

play00:18

that we use angular for creating

play00:20

client-side applications

play00:21

now we create these client-side

play00:23

applications with the help of components

play00:26

a component is basically a small piece

play00:28

of user interface

play00:30

when building an angular application

play00:31

first we build a bunch of components

play00:33

independent of each other and then we

play00:36

combine these components together to

play00:37

build a complex user interface

play00:40

every angular application has at least

play00:42

one component which is referred to as

play00:44

root component and mostly we call this

play00:46

root component as app component and this

play00:49

is just a convention you can call your

play00:51

root component anything but by

play00:53

convention we always call the root

play00:54

component of our angular application as

play00:56

app component this root component

play00:58

represents the entire application and it

play01:01

contains other child components so every

play01:04

angular application is essentially a

play01:06

tree of components and combining all

play01:08

these components together makes a

play01:10

client-side application with angular

play01:13

let's try to understand what is a

play01:15

component visually

play01:16

so here let's say we have a simple web

play01:19

application and in this web application

play01:21

we have a header we have the main

play01:23

content area we have a sidebar and we

play01:25

have a footer

play01:27

so while creating this application using

play01:29

angular we can split each of these UI

play01:31

paths into separate components and we

play01:33

can work on them independently for

play01:35

example we can split this header into a

play01:38

separate component and we can work on

play01:40

that component independently

play01:42

in this header component we have a nav

play01:44

bar so we can also split that navbar

play01:46

into a separate component and once that

play01:49

component is ready we can insert it in

play01:51

the header component

play01:52

in the same way if we want each menu

play01:55

item here in this neighbor we can create

play01:57

them as a separate component and then we

play01:59

can insert them into the navbar

play02:01

component

play02:03

then we can also have a separate

play02:05

component for the sidebar and in this

play02:07

sidebar we are using some sections so we

play02:10

can have a separate component for each

play02:12

of these sections and then we can insert

play02:14

these section components into the

play02:15

sidebar component

play02:17

then we can have a separate component

play02:19

for this main content area and in that

play02:21

main content area we can have separate

play02:22

components for these contents and then

play02:25

we can also have a separate component

play02:27

for footer

play02:28

and once we have all these components

play02:31

built we can combine all these

play02:33

components together to create a user

play02:35

interface

play02:37

so we can say that at the top we will

play02:40

have our app component app component is

play02:43

the root component of our angular

play02:44

application and in this app component we

play02:47

insert all our other components for

play02:50

example in this app component we can

play02:52

insert a component for header a

play02:54

component for never a component for main

play02:56

content area a component for footer and

play02:59

a component for sidebar

play03:01

then this sidebar component can have its

play03:04

own child components like popular

play03:06

courses 1 popular courses 2 popular

play03:08

courses 3 Etc in the same way the main

play03:11

content area can have its own child

play03:13

components like it can have a cover

play03:16

image it can have the main content it

play03:18

can have the Subscribe button so we can

play03:20

insert as many components as we want

play03:22

inside another component

play03:24

now in this example this app component

play03:27

is the root component and it is the

play03:29

parent of all the components in this

play03:31

case this app component is the parent

play03:33

for header component Neva component main

play03:36

component footer component and sidebar

play03:38

component

play03:39

in the same way this main component is

play03:41

the parent component for this cover

play03:43

component content component and this

play03:45

subscribe component so here we have a

play03:48

parent child relationship between the

play03:49

components

play03:51

all right so I hope with these examples

play03:53

now you understand what is a component

play03:55

now let's go ahead and let's create a

play03:57

component

play03:59

now in order to create and use a

play04:01

component we need to follow three steps

play04:03

first we need to create a typescript

play04:05

class and we need to export it second we

play04:08

need to decorate that typescript class

play04:10

with ADD component decorator and finally

play04:12

in order to use that class as a

play04:14

component we need to declare it in the

play04:16

main module file so let's go ahead and

play04:19

let's do that

play04:21

here in vs code let's first go to this

play04:23

Source folder in that Source folder we

play04:25

have this app folder and this app folder

play04:27

is our main application folder

play04:30

in this app folder we have a component

play04:32

called app component

play04:34

and this is going to be our root

play04:35

component okay now whatever application

play04:39

code we will write we will write it

play04:40

inside this app folder so for example

play04:43

let's say I want to create a component

play04:45

called header now we are going to insert

play04:47

that header component inside our root

play04:49

component which in this case is app

play04:51

component

play04:52

and since we are going to insert it in

play04:54

our app component the convention is the

play04:56

child component should always go inside

play04:58

the parent component folder in this case

play05:01

the parent component folder is this app

play05:03

component folder and we are going to

play05:05

insert the header component inside the

play05:07

app component so the header component is

play05:10

going to be the child component of app

play05:12

component so we are going to create a

play05:14

new folder inside this app folder

play05:17

and here we are going to name this

play05:19

folder with the name of the component so

play05:21

I am going to create a header component

play05:23

so here I will call this folder as

play05:25

header

play05:29

now inside this header folder we are

play05:31

going to create our header component

play05:34

and keep in mind that a component is

play05:36

nothing but a typescript class

play05:38

so inside this header folder we are

play05:41

going to create a typescript file let me

play05:43

go ahead and let me create that file and

play05:45

this file is going to contain the header

play05:48

component class

play05:49

so here the convention to name your

play05:51

component is first you need to specify

play05:53

the name here we want to call our

play05:55

component as header then you need to use

play05:58

Dot and you need to call it component

play06:00

and since it is going to be a typescript

play06:03

file we need to specify the extension as

play06:05

dot TS

play06:06

okay so first the component name dot

play06:10

component and then the extension

play06:12

so this file is going to contain our

play06:14

component class

play06:17

okay so file has been created now inside

play06:20

this file in order to create a component

play06:22

first we need to create an exporter

play06:24

class

play06:25

in order to create a class in typescript

play06:27

we use class keyword and then we specify

play06:30

the name for the class and the

play06:32

convention here is the name for the

play06:34

class should be the name of the

play06:36

component in this case we want to call

play06:37

the component as header and it should be

play06:39

prefixed with component

play06:43

okay

play06:44

so this is how we create a typescript

play06:47

class now we also want to use this

play06:49

typescript class in other files so we

play06:52

also need to export it from here so that

play06:54

we can import it in other files

play06:57

now this class here it is a simple

play06:59

typescript class it is not a component

play07:01

class

play07:02

in order to make this class a component

play07:04

class we need to decorate it with at

play07:07

component decorator

play07:10

okay and in order to use this component

play07:13

decorator we need to import it from

play07:16

angular slash core Library

play07:18

so here first we are importing this

play07:20

component from angular slash code

play07:22

library and then we are using it

play07:25

now to this decorator we need to pass

play07:27

the metadata object

play07:29

and in there we need to specify few

play07:32

properties so as you can see here we are

play07:35

already seeing a red squiggly that means

play07:36

we have some error here and it says

play07:39

component is missing a template so as we

play07:42

have learned earlier every component

play07:44

must have a view template

play07:46

okay so here we are going to specify two

play07:48

properties the first property is going

play07:50

to be selector

play07:51

and we are going to call this selector

play07:54

as app hyphen the component name which

play07:57

is header so it is a convention that you

play08:00

call you selector app hyphen and then

play08:03

the component name

play08:05

once we have the selector we also need

play08:07

to specify the view template now in

play08:10

order to specify the view template there

play08:11

are two ways we can either use template

play08:14

property or we can use template URL

play08:16

property for now we are simply going to

play08:19

use template property

play08:21

okay and to this template property we

play08:24

can assign some string and inside that

play08:26

string we can write some HTML so here

play08:28

I'm going to write an HTML let's say I'm

play08:30

going to write an S3 element there

play08:32

and in that S3 element I'm going to

play08:35

specify the name of this application

play08:36

which is going to be ecart

play08:39

and that should be it so here I want to

play08:41

create a very simple component now the

play08:43

component has been created okay but

play08:46

angular is not aware about this

play08:48

component yet

play08:49

so what we need to do is we need to tell

play08:51

angular that we have a component called

play08:53

header component and whenever the

play08:56

angular application loads it should know

play08:58

about this component

play09:00

do that we need to go to our main module

play09:03

class

play09:03

and in here we need to declare our

play09:06

custom component

play09:07

so here we need to declare the header

play09:09

component which we have just created

play09:11

in order to do that first we need to

play09:14

import this header component

play09:16

so for that we can use this import

play09:19

keyword and then what do we want to

play09:21

import we want to import header

play09:24

component

play09:25

okay and we want to import it from

play09:27

this path so in the app component we

play09:31

have this header folder and in that

play09:32

header folder we have this header

play09:34

component okay here dot TS is not

play09:37

required

play09:38

and once we have imported it now we need

play09:41

to declare it inside this declarations

play09:44

array so let me go ahead and let me copy

play09:46

this component and we can declare it

play09:48

inside this declaration sorry

play09:50

and now we should be able to use this

play09:52

header component

play09:54

so let's go to our app component.html

play09:57

which is the root component of our

play09:58

application and in there I am simply

play10:01

going to use our header component now in

play10:04

order to use header component we need to

play10:06

use it selector so what is the selector

play10:07

we have specified for this header

play10:09

component it is app header so let's copy

play10:12

this selector

play10:14

let's go to app component.html and there

play10:17

let's use that selector as an HTML

play10:19

element

play10:20

and that should be it if I save the

play10:22

changes and if I go back to the browser

play10:26

you can see ecut here so this content it

play10:30

is coming

play10:31

from our header component

play10:34

from this view template

play10:36

so here we have used this template

play10:38

property in order to specify the view

play10:40

template for this header component class

play10:42

so this S3 element is being rendered in

play10:45

the browser

play10:46

here okay so this is how we create and

play10:49

use a component in angular

play10:52

first we create a typescript class and

play10:54

in order to make that typescript class a

play10:56

component class we decorate it with at

play10:58

component decorator

play10:59

and then there we need to specify a

play11:01

selector and a view template if we don't

play11:04

specify the view template you will see

play11:05

that we have an error

play11:06

as you can see we have an error

play11:09

so let's get it back

play11:10

so we need to specify the selector and

play11:12

the view template and then in order to

play11:14

use this component class in our

play11:17

application first we need to declare it

play11:19

so that the angular will know about this

play11:21

component and to declare it we need to

play11:23

go to our module class so in this case

play11:26

we have only one module class this app

play11:28

module class and there it is decorated

play11:30

with this NG model decorator and in

play11:33

there we have this declarations array so

play11:35

we need to register we need to declare

play11:37

our component inside this declarations

play11:40

array

play11:42

all right so this is all from this

play11:43

lecture if you have any questions then

play11:44

feel free to ask it thank you for

play11:46

listening and have a great day

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Angular ComponentsWeb DevelopmentJavaScript FrameworkClient-side AppsTypescript ClassesApp ComponentUI DesignFront-endAngular TutorialModule Declaration