#2: ๐Ÿ…ฐ Angular kicks in

Before we start to develop our app, let's look at the project and how Angular gets in the picture. Open the todo-list folder in your IDE if you haven't done so already, so we can inspect the files. All the relevant files at this stage exist inside the src folder. If you're really eager to start coding, you can skip this chapter and come back to it later to understand the project structure.

Open the file index.html. The content that is rendered in the browser's window is everything you see inside the <body> element. All you can see there now is another, non-HTML element: <app-root></app-root>. This element is actually an Angular Component, defined in the file app/app.component.ts with the class named AppComponent. (We'll take a look at it in the next chapter).

Angular can be defined in many ways. One of them is JavaScript code which runs when the application is presented in the browser. All the code you will write - components, modules, services, etc. are recognized by Angular. Angular will perform actions accordingly. For example, components you will write and use will compile to JavaScript functions. These functions insert the component content into the DOM (Document Object Model) - the representation of the page structure in the computer's memory, which the browser uses to show the application. That's how you'll see the component you created on the screen.

So <app-root> is not an HTML element, it is an Angular Component. When the application is ready, the content of the component is inserted inside the <app-root> tag, and you will see it in the browser:

Let's learn a bit about Angular applications' main building block - the Angular module.

NgModule

Angular needs us to define what we want it to compile. For this we define Angular Modules, or NgModules, which are like packages of related things. These packages can include components, services, directives, pipes and other NgModules. We already have a root NgModule defined for us in the file app/app.module.ts. Let's take a look at this file.

The last line in the file defines a JavaScript class:

export class AppModule { }

export is a reserved word in JavaScript. It exposes whatever is defined after the export keyword, in this case the class AppModule, so that other files can use the exposed code using the import statement. You can see examples of classes and functions imported from other files in the first lines in this file. These are used in the file.

The class AppModule is empty. It will get its functionality from Angular, which will identify its role by the code preceding this line, starting with @NgModule({.

Every entity in Angular (NgModule, components, services, directives and pipes) is just a class with a decorator. The decorator tells Angular what the role of this class is.

@NgModule({...}) is a decorator. A decorator is just a function. When using it we put @ before its name. This way it becomes a decorator: it looks at what is written right after the function call and receives it as an argument. Decorators usually do something with what they decorate. For example, in this case NgModule receives the AppModule class and adds to it methods and members that later on will be used by Angular. This way Angular will recognize that this class represents an NgModule.

What we pass into the decorator function is used by Angular to decorate the class. You can see we pass an object with members, each member is a list of other classes. We'll explain shortly what each member represents.

declarations: a list of Angular things that are relevant in this module. They may use each other (for example, a directive used in a component). We pass here only one component - AppComponent, because that's all we have in our application right now.

imports: a list of other NgModules which are needed for this module. For example, we may use things from FormsModule - directives and services, inside the AppComponent.

bootstrap: this member is relevant only to the root NgModule. You will not find it in the modules in the imports list for example. It tells Angular which component should be used as the root component of the application. Every component can use other components in its template. We have one root component that starts the whole structure. So we actually get a tree structure of the components that build our application. In this case, the root component is AppComponent (with the selector app-root). We saw it used in index.html as the only component inside the <body>.

How does Angular know that the AppModule is the root NgModule? This is defined in the file main.ts in the last lines:

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.log(err));

We bootstrap our root NgModule to a renderer. This way we tell Angular what NgModule to use as the starting point of our application. And we also choose a renderer: platformBrowserDynamic. It knows how to take our code and add the relevant data (elements, attributes, etc.) to the browser's DOM.

If there's an error, it is caught and logged in the browser's console (seen only when the browser's dev-tools are open).

We can use a different renderer at this point, for example one that renders to Android or iOS native elements! We just need a renderer that knows how to take our templates (which use HTML notations) and JavaScript code, and create native mobile application elements. An example to such a renderer is NativeScript by Telerik.

There are even renderers to Arduino, with which you can connect sensors, buttons, LEDs and other hardware to your application! You can see a great example for this here: Building Simon with Angular2-IoT.

We've seen how we tell Angular where and how to start its work, how we define the root module and the root component, and how we use the root component.

In the next chapter we'll see how a component is defined in Angular.

Last updated