Special welcome gift. Get 50% off your first courses with code “AS50”. Find out more!

HomeInterviewAngularJS Q/AHow does an Angular application work?
about us new 1

How does an Angular application work?

Every Angular app contains a file named angular.json. This file contains all the configurations of the app. While building the app, the builder looks at this file to find the application’s entry point. See the structure of the angular.json file:

  1. “build”: {  
  2.   “builder”: “@angular-devkit/build-angular:browser”,  
  3.   “options”: {  
  4.     “outputPath”: “dist/angular-starter”,  
  5.     “index”: “src/index.html”,  
  6.     “main”: “src/main.ts”,  
  7.     “polyfills”: “src/polyfills.ts”,  
  8.     “tsConfig”: “tsconfig.app.json”,  
  9.     “aot”: false,  
  10.     “assets”: [  
  11.       “src/favicon.ico”,  
  12.       “src/assets”  
  13.     ],  
  14.     “styles”: [  
  15.       “./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css”,  
  16.       “src/style.css”  
  17.     ]  
  18.   }  
  19. }  

When the application enters the build section, the options object’s main property defines the entry point of the application. The application’s entry point is main.ts, which creates a browser environment for the application to run and calls a function called bootstrapModule, which bootstraps the application.

These two steps are performed in the following order inside the main.ts file:

  1. import { platformBrowserDynamic } from ‘@angular/platform-browser-dynamic’;  
  2. platformBrowserDynamic().bootstrapModule(AppModule)   

In the above line of code, AppModule is getting bootstrapped.

The AppModule is declared in the app.module.ts file. This module contains declarations of all the components.

Below is an example of app.module.ts file:

  1. import { BrowserModule } from ‘@angular/platform-browser’;  
  2.      import { NgModule } from ‘@angular/core’;  
  3.      import { AppComponent } from ‘./app.component’;  
  4.      @NgModule({  
  5.        declarations: [  
  6.          AppComponent  
  7.        ],  
  8.        imports: [  
  9.          BrowserModule  
  10.        ],  
  11.        providers: [],  
  12.        entryComponents: [],  
  13.        bootstrap: [AppComponent]  
  14.      })  
  15.      export class AppModule { }  

In the above file, you can see that AppComponent is getting bootstrapped. It is defined in app.component.ts file. This file interacts with the webpage and serves data to it.

Below is an example of app.component.ts file:

  1. import { Component } from ‘@angular/core’;  
  2.       @Component({  
  3.         selector: ‘app-root’,  
  4.         templateUrl: ‘./app.component.html’,  
  5.         styleUrls: [‘./app.component.css’]  
  6.       })  
  7.       export class AppComponent {  
  8.         title = ‘angular’;  
  9.       }  

Each component is declared with three properties:

  1. Selector – It is used to access the component.
  2. Template/TemplateURL – It contains HTML of the component.
  3. StylesURL – It contains component-specific stylesheets.

Now, Angular calls the index.html file. This file consequently calls the root component that is app-root. The root component is defined in app.component.ts.

See how the index.html file looks like:

  1. <!doctype html>  
  2.   <html lang=”en”>  
  3.   <head>  
  4.     <meta charset=”utf-8″>  
  5.     <title>Angular</title>  
  6.     <base href=”/”>  
  7.     <meta name=”viewport” content=”width=device-width, initial-scale=1″>  
  8.   </head>  
  9.   <body>  
  10.     <app-root></app-root>  
  11.   </body>  
  12.   </html>  

The HTML template of the root component is displayed inside the <app-root> tags.This is the way how every angular application works.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

Categories

ads sidebar 1

You May Also Like

Oracle has several modes for shutting down the database: In normal mode, the database is shut down by default. It...
Materialized views are items that contain condensed sets of data from base tables that have been summarized, clustered, or aggregated. They...
Every database in Oracle has a tablespace called SYSTEM, which is generated automatically when the database is created. It also...