Hello World Application - Angular 2 - Part Four

Hi Friends. In our previous article, we understand the application files in angular 2 application.

Hi Friends. In our previous article, we understand the application files in angular 2 applications.

For reference, please read my previous article for clear understanding.

Now we are going to understand, how our hello world application works and its execution process.

Two files are going to be concepts for us in this articles, they are,

  • App.component.ts
  • Main.ts

Now let’s start with app.component.ts: If we recollect fromm our previous article, we mentioned that component is nothing but a class and some meta data as shown below

  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. @Component({  
  5.     selector: 'my-app',  
  6.     template: `<h1>Hello World</h1>`,  
  7. })  
  8. export class AppComponent {}  

Meta data here is selector and template, which is indicated by a component decorator.

Now, a decorator is a feature of type script and this is going to attach this meta data to class underneath it.

Selector: It is an html tag that we are going to use in order to have component in html tag.

Template: it is an html tag that has to go in between selector tags.

Let's open index.html, we have the my app tag as shown below,

  1. <!-- 3. Display the application -->  
  2.   
  3. <body>  
  4.     <h4>header4 from index.html</h4>  
  5.     <my-app>Loading...</my-app>  
  6. </body>  
This is a custom tag, nothing but a selector for our component and between these two tags we are going to be replacing hello world.

Now go back to app.component.js, here we also need to specify that we are importing component class from angular/core module as shown below.

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

Now, Open main.ts, this is going to bootstrap app.component, bootstrap means it is going to start our app.component. How is it going to do that ?

This going to import bootstrap function from angulars/platform and then it’s going to import app.component class.

Overall, what we have done means,

  • We Import bootstrap component
  • We import app.component
We just bootstrap app.component as shown below, 
  1. import { bootstrap } from '@angular/platform-browser-dynamic';  
  2. import { AppComponent } from './app.component';  
  3. bootstrap(AppComponent);  

Now we are ready for use what we required, Let’s see how the flow of execution works inorder to display hello world.

In Index.html, you can see System.import line as shown below

  1. <script>  
  2.    System.import('app').catch(function(err){ console.error(err); });  
  3. </script>  

Open systemjs.config.js, in package, our main file is main.js as shown below

  1. // packages tells the System loader how to load when no filename and/or no extension  
  2. var packages = {  
  3.     'app': {  
  4.         main: 'main.js',  
  5.         defaultExtension: 'js'  
  6.     },  
  7.     'rxjs': {  
  8.         defaultExtension: 'js'  
  9.     },  
  10.     'angular2-in-memory-web-api': {  
  11.         defaultExtension: 'js'  
  12.     },  
  13. };  
Now open main.ts, it is going to bootstrap app.component as shown below
  1. import { bootstrap } from '@angular/platform-browser-dynamic';  
  2. import { AppComponent } from './app.component';  
  3. bootstrap(AppComponent);  

So open app.component.ts, just for reference open index page , the flow of execution sits in the body tag, now it encounters the my-app html tag, this my-app recognizes that its nothing but a selector and component, then its going to replace with Hello world as shown below.

 

In our next article, let us create a component and understand more about component in details, Thanks for reading my article --  stay tuned.