Creating Components in Angular
Creating Components in Angular
ASP.NET Core
In the HTML you define the HTML element the operations you want to
perform and in the Typescript, you have the operations, properties and
so on. If we take a look at the counter component example we have
HTML here and also we have this variable current count and we have
Typescript file that does some operations and has the properties.
Now to use a component you have two options either take its selector
and use it as an HTML element where ever you find suitable or define
routing for it inside the router module.
If you take to look into the nav-menu component and notice its selector.
If we go to the app-component HTML which is the main entrance for
our obligations. You can see we have nav-menu used as an HTML tag,
and now we can use it to throw out the obligation where ever we want.
Creating a Component.
To create a new component we have two options we can simply copy
this to create a new component.
Or you could use the help of the Angular CLI now I am going to open a
new command line and then I am going to call angular CLI you can in
the image below how to do.
When we say skip import It means we don’t want to place the
component inside the module file we don’t put this command inside the
app folder we will put it inside the client app folder and you will notice
that it generate a new component and we can see it comes with four files
the CSS file, HTML, spec and Typescript as given below.
Now let’s come to the HTML and create a new to-do list. This is a
simple to-do list component. As you can see in the picture below that I
have add buttons for my list Now I want to iterate through the to-do
items that I have created I'm going to create just the table and I'm going
to create a new table that says that this is my to-do item.
I'm going to create a new table raw and that table raw will iterate
through to-do items. I have my application right here. I have this input
that I have. I have this button that has also this event to be clicked and
we're going to iterate through them.
Let's go into Typescript and declare it a new to-do area which we will
iterate through and this shall be of type array of string we're going to add
to-do pops. And as you can see it's quite simple and it's not that likely
does not take too much effort. So we have defined the property we have
defined the area. So now we have done everything that we need to do.
We need to add this reference to the app module we need to add it to the
declaration.
Let’s run this and now you'll notice that we can see the new component
and as well as the elements inside it. So as you can see this is how we
can create a component we need.
I hope this article helps you keep learning and sharing. Thanks.