Learning Angular: Exploring Components in Angular (Part 3)

Posted by


In Angular, components are the building blocks of your application. They are responsible for controlling the display logic and behavior of a specific piece of your user interface. In this tutorial, we will look at everything you need to know about components in Angular.

Creating a Component:
To create a new component in Angular, you can use the Angular CLI to generate it for you. Simply run the following command:

ng generate component myComponent

This will create a new folder named myComponent in your project directory, containing all the files needed for the component, including the component class, template, and style files.

Component Class:
The component class is the main part of your component. It contains the data, methods, and properties that control the behavior and logic of your component. Here is an example of a simple component class:

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

@Component({
  selector: 'myComponent',
  templateUrl: 'myComponent.component.html',
  styleUrls: ['myComponent.component.css']
})
export class MyComponent {
  title = 'Hello, World!';
}

In this example, we have created a new component class with a property called title that contains the value "Hello, World!".

Component Template:
The component template is the HTML markup that defines the structure of your component. You can use interpolation to bind data from your component class to your template. Here is an example of a simple component template:

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

In this example, we have used interpolation to display the value of the title property from our component class.

Component Selector:
The selector is a CSS selector that defines how your component will be used in the HTML markup. Here is an example of a component selector:

@Component({
  selector: 'myComponent',
  templateUrl: 'myComponent.component.html',
  styleUrls: ['myComponent.component.css']
})

In this example, we have defined the selector as myComponent, which means we can use this component in our HTML markup by using the tag <myComponent></myComponent>.

Component Styling:
You can also add styling to your component by using CSS or SCSS files. The styles defined in these files will only apply to your component and will not affect other parts of your application. Here is an example of a component style file:

h1 {
  color: red;
}

In this example, we have defined a style for the <h1> element in our component template to change its color to red.

Using Components:
To use a component in your Angular application, you need to import the component class in your module file and add it to the declarations array. Here is an example of how you can use the MyComponent in your AppModule:

import { MyComponent } from './myComponent/myComponent.component';

@NgModule({
  declarations: [
    MyComponent
  ]
})
export class AppModule { }

After adding the component to the declarations array, you can now use the component in your HTML markup by using its selector. Here is an example of how you can use the MyComponent in your HTML markup:

<myComponent></myComponent>

And that’s it! You have now learned everything you need to know about components in Angular. Components are a crucial part of building Angular applications, and understanding how to create, use, and style them is essential for developing great applications. I hope this tutorial has been helpful to you in learning about components in Angular. Happy coding!

0 0 votes
Article Rating
18 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Angular
2 months ago

Watch more episodes of Learning Angular→ https://goo.gle/Learning-Angular

@tuba-inxs
2 months ago

Great video! at https://youtu.be/R0nRX8jD2D0?t=164 , <img> looks like a nested child of <header>, not a sibling.

@alissabrave424
2 months ago

I did everything same with the tutorial but my logo does not show up, I do not understand where the problem is

@edemmarvinkudjo9346
2 months ago

I love Angular❤

@CrazyOsi
2 months ago

not the best idea to create a website about HOMEs and have a HOME-component

@stevofootballhome6786
2 months ago

the the set up is no longer working. pls assist me with the first step setup

@Guidje
2 months ago

damn, nice content!! i wasn't expecting to much (because it's a beginner tutorial) but this is really well done and the host didactic is pretty good

@djrandomoficial4901
2 months ago

03:04 is it normal to use 'aria-hidden' together with 'alt' in the html element? semms no sense.

@MrBingo37
2 months ago

3:08 header with logo will not compile ,it seems that it does not work with Angular 17+

———————————————————
Angular CLI: 17.1.3
Node: 20.11.0
Package Manager: npm 10.4.0
OS: linux x64

@COOKWITHARMAANNN
2 months ago

not working angular 17 and above

@siva_geddada
2 months ago

Hi team can anyone tell me , what is the font used in this video, https://youtu.be/LZxbDp1nOVo?si=hzuVdCz4RgKkX1n2

@TrashCanSystem
2 months ago

I'm so glad this is working so far, I say this because usually Youtube tutorials almost never works, lol

@darksorcerer1575
2 months ago

I would recommend that if anyone gives a thumbs up on this to actually try doing this first.

@darksorcerer1575
2 months ago

I tried this on 2 different computers and can't get passed the part where it should show the logo. I even tried converting the .svg image to a .png, but no change. I'm using Visual Studio 2022 Pro, no VS Code, but I don't think that would have anything to do with this. No errors detected with the typescript code. I did notice that the video doesn't match watch the presenter is saying about the location of app.component.ts in the previous video though. Any suggestions would be great.

I just also wanted to say that the image displays no problem in it's own tab: "http://localhost:4200/assets/logo.svg&quot;

I tried continuing to see if the issue would resolve itself, but even after creating the home component via "ng generate component Home –standalone –inline-template", it just shows this compile error: Compiled with problems:

×

ERROR

src/app/app.component.ts:18:14 – error NG2010: 'imports' is only valid on a component that is standalone.

18 imports: [HomeComponent]

~~~~~~~~~~~~~~~

src/app/app.component.ts:20:14

20 export class AppComponent {

~~~~~~~~~~~~

Did you forget to add 'standalone: true' to this @Component?"

==============
here's my angular cli info:
Angular CLI: 16.2.2

Node: 18.18.0

Package Manager: npm 9.8.1

OS: win32 x64

Angular:

Package Version

——————————————————

@angular-devkit/architect 0.1602.2 (cli-only)

@angular-devkit/core 16.2.2 (cli-only)

@angular-devkit/schematics 16.2.2 (cli-only)

@schematics/angular 16.2.2 (cli-only)
===========================

Not sure if because I'm using a newer version that this is happening, but it doesn't look like I can continue using this tutorial. From my further investigation: The standard Angular framework doesn't support the imports property directly inside the @Component decorator for component-to-component imports.

I do not recommend this tutorial as it has consumed a great deal of my time on 2 different computers and now I need to start over again using a different tutorial.

@Eltopshottah
2 months ago

When generating the home component I get an error ‘Could not find NgModule. Use the ‘—skip-import’ option to skip importing in NgModule…

@jediampm
2 months ago

Hi, i dont know why you are starting to show only inline templates.
At least you should show in home template how to do a no inline template.
And i think in any component has too much html, as such you should be separate template in file anyway.

@TayambaMwanza
2 months ago

I think at 1.26, when you're describing the different parts that will be broken up into components, it would be helpful to highlight the areas or outline them to give a nice visual indicator of each component/section.

@elyoaprogrammer
2 months ago

Excellent Angular series for beginners. I would love watch similar tutorials from the Angular team with more advanced topics. Thanks