Advertisement
  1. Code
  2. Angular
Code

Beginner's Guide to Angular 4: Components

by
Difficulty:BeginnerLength:ShortLanguages:
This post is part of a series called Beginner's Guide to Angular 4.
Beginner's Guide to Angular 4: Services

Angular is a popular framework for creating front ends for web and mobile applications. It all started with AngularJS 1.x and then AngularJS 2, and now it's finally Angular, with the latest updates and bug fixes being worked on by the Angular team.

Components are an important part of an Angular web application. In this tutorial, you'll see how to get started with creating a web application using Angular, and you'll also get to know components.

Getting Started

Get started by installing Angular CLI using the node package manager (npm).

Once you have the Angular CLI installed, create a new Angular app using the CLI.

Navigate to the application folder and start the server.

Point your browser to http://localhost:4200/ and you should have the default app running.

Angular App Structure

Navigate to the angular-app project folder and have a look at the project structure. Here is how it looks:

Angular App Project Structure

Every Angular app has a root module where you define the main component to load. In the default Angular app, the root module is defined inside the app.module.ts. When the AppModule loads, it checks which component is bootstrapped and loads that module. As seen in the app.module.ts, the module which is bootstrapped is AppComponent. The AppComponent component is defined in the file app.component.ts.

A component is defined using the @Component decorator. Inside the @Component decorator, you can define the component selector, the component template, and the related style

What Is an Angular Component?

Components are like the basic building block in an Angular application. Components are defined using the @component decorator. A component has a selector, template, style and other properties, using which it specifies the metadata required to process the component.

From the official docs:

Components are the most basic building block of an UI in an Angular application. An Angular application is a tree of Angular components. Angular components are a subset of directives. Unlike directives, components always have a template and only one component can be instantiated per an element in a template.

The best way to understand something related to programming is by actually doing. So let's start by creating an Angular component for adding two numbers. Let's call it CalculatorComponent.

Creating the Calculator Component

Let's start by creating a component for our calculator. Inside the src/app folder, create a folder called calc. This is where our calc component will reside. Inside the calc folder, create a file called calc.component.html. This will be the template for our calculator component. Here is how it looks:

Create a file called calc.component.ts. This is where you'll define the calc component and specify the related metadata. You'll be defining the component using the @component decorator. To define the component, you need to import the component module from angular core.

Define the component by specifying the template, style, and selector. You'll also define a class to manage the template specified by the @component decorator. Here is how it looks:

All styles related to the component template should be defined inside the file specified in the component decorator. So create a file called calc.component.css inside the calc folder. You'll put the style for the calculator component inside this file.

Now that you have your component ready, let's define the component inside the root module app.module.ts

First import the component inside the app.module.ts file and then include it in the declarations section. Here is how it looks after adding the CalcComponent:

As you can see in the root module app.module.ts, the AppComponent is the bootstrapped module and hence it will render by default. So, to view our calculator component, define it inside the app.component.html. Here is how the app.component.html file looks:

Save the above changes and start the server. You should be able to see the HTML content of the calculator component displayed.

Adding the Calculator Functionality

Let's start by adding a template for our Angular calculator. Add the following code to the calc.component.html file:

Add the following style to the calc.component.css file.

Save the above changes and you should be able to view the following user interface.

Calculator Component Interface

Let's add the ngModel directive to the above displayed input text boxes. Modify the calc.component.html code as shown below:

As seen above, you have set the ngModel for the input text boxes to the variables number1 and number2

Let's define the variables inside the CalcComponent in the calc.component.ts file.

Now, when the user types into the text boxes, the corresponding ngModel variable gets updated. You can check by displaying the variable in the component's template file.

Save the changes and enter values inside the input boxes, and you should have the data updated inside the span.

Calculator Component

Let's add a button click to the Add button which will calculate the sum of the number1 and number2 when clicked on the button.

Modify the HTML code as shown to include the click directive.

Define the add function inside the CalcComponent as shown:

As seen in the above code, the result of the addition is being placed in a variable called result.

Let's modify the HTML template to display the result once the variable is set.

Save the above changes and try to add two numbers by clicking on the Add button. You will have the result displayed in the user interface.

Calculator Component Result

Wrapping It Up

In this tutorial, you saw how to get started with creating a web app using Angular 4. You learnt about Angular components and how to create one. You created a simple Angular component to add two numbers.

Source code from this tutorial is available on GitHub. If you’re looking for additional resources to study or to use in your work, check out what we have available on Envato Market.

Do let us know your thoughts, suggestions or any corrections in the comments below.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.