In today’s post, we will continue with the introduction to Angular. As we already know, Angular is a framework dedicated to building web applications on the client side, which means that the behavior and rendering of our “web app” are handled by the browser.

Let’s take a look at a diagram illustrating the architecture of Angular and how different elements relate to each other.

Angular architecture - Image from angular.io

To understand this diagram, let’s classify the elements that make up Angular into three main blocks:

In this post, we will focus on the first two blocks.

Modules

Angular modules (NgModules) are quite different from JavaScript ES6 modules, although we’ll also use the latter to manage library imports.

As we know, Angular applications are modular, meaning they consist of various independent blocks, each of which contains a part of the application or a set of its behaviors.

Every application should have at least one NgModule class, usually named AppModule, and its file is typically named app.module.ts.

// ES6 imports
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SomeProvider } from './providers/some_provider'

@NgModule({
  imports: [BrowserModule],
  providers: [SomeProvider],
  declarations: [AppComponent],
  exports: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}

This would be an example of the root module of an Angular application. We import libraries using JavaScript and encapsulate them within the @NgModule decorator. As you can see, the decorator has a set of metadata or properties. Let’s define the most important ones; you can find the rest in the official Angular documentation.

Components

A component controls an area of the screen called the view. A component is a JavaScript (ES6) class with the @Component decorator.

Let’s take a look at a real example of what a component is. For this, we’ll take a screenshot from Instagram and break it down into different components.

Instagram example

A component includes properties and methods available for its template, but it’s important to be aware that not all logic should reside in this class. We should include everything related to the controller of the view in this class and abstract all other methods into services that will be injected later.

Let’s take a look at the code of the AppComponent, which is our main component in our “FirstProject”.

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  title = 'First Project'

  constructor() {}

  ngOnInit() {}
}

First, let’s talk about the metadata of the @Component decorator.

This example showcases some of the most important and commonly used attributes:

You might also wonder why the class implements the OnInit interface. This is related to the lifecycle of Angular components. In this case, the ngOnInit method inherited from that interface will be executed when the component is created. In this post, we won’t go into detail about the lifecycle; we’ll cover it in depth in an upcoming post. Here’s the documentation for the curious ones 🤔.

Template

The template is responsible for defining the view of our component. In the case of Angular, it’s a traditional but enhanced HTML with a set of expressions and directives that improve its behavior and make our lives easier.

In a component’s template, besides the regular HTML tags, you can find other distinct elements that Angular uses.

In upcoming posts, we will analyze each of these in detail. For now, let’s take a look at the template of our main component in “firstProject”.

<h1>My First Project: {{title}}</h1>

We execute the command ng serve in our terminal to see the result, and we would get the following:

State of firstProject displayed

We can see that it has inserted the value of the “title” variable from our component into the DOM. In Angular, this is called “interpolation,” and it’s very useful for creating web applications using this wonderful framework.

In the upcoming posts, we will discuss more about the syntax of the template and learn while programming a web application for a to-do list. After all, the best way to learn programming is by programming, if you’ll pardon the redundancy 😄.

See you here again soon, greetings everyone ✋.

More related posts
Angular: File structure and first component. Angular: File structure and first component.

Angular: File structure and first component.

Date
Angular: File structure and first component. Angular: File structure and first component.

Angular: File structure and first component.

Date