Jumpstart Angular 6.0

In today's’ web development world, AngularJS is known as one of the most important and widely-used frameworks. Recently, Angular version 6.0  has launched, so in this document, we will discuss how to develop the first component or program in Angular 6.0.

AngularJS

AngularJS is an open-source JavaScript-based framework. It was actually developed by Misko Havery & Adam Abrons in 2009. This framework supports the JavaScript-based MVC (MVVM) framework. As per Google, the definition of Angular is below:

“AngularJS is a structural framework for dynamic web applications. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application components clearly and succinctly.”

The most common advantages for which developers like to use Angular in their projects are –

  • It supports MVC concepts.
  • It always supports SPAs (Single Page Applications).
  • It supports client-side templating.
  • In this framework, we can perform unit testing very easily.

Prerequisites for AngularJS

For starting the development in Angular 6.0, we need the followings perquisites installed on our PC.

  • Install Node.js
  • Install TypeScript 2.7 or above
  • Microsoft Visual Studio or Visual Studio Code for writing the code
  • Install lite-server (in case you are using Visual Code) to host and run the application

So, before going to start Angular 6.0, we first need to know how to install TypeSript tool. For installing TypeScript, we first need to install Node.js. The latest version of NodeJS can be downloaded from the below URL.

https://nodejs.org/en/

For installing TypeScript, we can download the latest version of TypeScript either by using command line argument using node.js or if we use Visual Studio, then we can directly download it from NuGet Package Manager.

Command line for installing TypeScript,

npm install -g typescript

Command line for installing Lite-Server

npm install -g lite-server

Project Configuration

Now, we will discuss how to set the environment of an Angular 6 project. In this part, we will develop our first program in Angular 6. Before going to start the program, let’s discuss the project configuration files which are required in Angular. An Angular project always contains 3 major configuration files. They are,

  1. tsconfig.json
    This file contains the compiler options which are required to run the project.

  2. package.json
    It is basically a JSON file which contains all the necessary information regarding different packages which we require to run or execute the Angular application.

  3. system.config.js
    This file acts as a configuration file. This file is used to load the node modules which are compiled in TypeScript at the time of execution.

What is a Component?

The Angular framework is basically a component-based framework (from Angular 2.0 or above version). So before starting to code  in Angular 6, we first need to understand the concept of a component and how it can be defined. So in Angular, a component is just a class like other OOPs based languages. This class is basically defined to display any element on the application screen. So, as per our requirement, we can create, update or delete any existing component in the application. In TypeScript, we can define the component class with the help of @Compoennt() decorator.

Component Configuration

@Component decorator actually decorates a TypeScript class as a component object. It is a function which takes different types of parameters. In the @component decorator, we can assign different property values to fix the behavior of the components. The most used properties are as below.

  • selector
    A string value which represents the component on the browser at the execution time.

  • template
    The value of this property basically contains the basic HTML code which we will need to display it in the browser. It acts as an inline template.

  • templateUrl
    It is actually another way of defining the HTML tags in the component. We can create HTML files with proper HTML tags and then need to provide that file name with the relating path in this attribute. So that at the time of execution, the Angular loader will display the HTML codes in the browser for the applications. Some people call this phenomenon "External Templating" too.
  • moduleId
    It is used to resolve the related path of template URL or style URL for the component objects.
  • styles or stylesUrls
    It is used to provide the style of the components. We can define inline CSS using styles attribute or can provide CSS file with related URLs in the styleUrls attribute.

  • providers
    This attribute is primarily used for dependency injection purpose. We can inject services, packages, components etc. using this attribute.
Create Angular Module

As we already discussed earlier that everything in Angular 6 belongs to an Angular Module, for developing the root component, we first need to declare our first Angular module. An Angular module can be defined by creating a TypeScript class decorated with the “NgModule” decorator. In order to use it, we first need to import it.

  1. import { NgModule } from '@angular/core';  
  2.   
  3. @NgModule()  
  4.   
  5. export class SampleModule { }  

tsconfig.json File

  1. {  
  2.   "compileOnSave"true,  
  3.   "compilerOptions": {  
  4.     "target""es5",  
  5.     "module""commonjs",  
  6.     "moduleResolution""node",  
  7.     "sourceMap"true,  
  8.     "emitDecoratorMetadata"true,  
  9.     "experimentalDecorators"true,  
  10.     "removeComments"false,  
  11.     "noImplicitAny"false,  
  12.     "declaration"false,  
  13.     "noStrictGenericChecks"true,  
  14.     "typeRoots": [  
  15.       "node_modules/@types"  
  16.     ],  
  17.     "lib": [  
  18.       "es2017",  
  19.       "dom"  
  20.     ]  
  21.   },  
  22.   "exclude": [  
  23.     "node_modules",  
  24.     "wwwroot"  
  25.   ]  
  26. }  

package.json File

  1. {  
  2.   "name""angular-sample",  
  3.   "version""6.0.0",  
  4.   "private"true,  
  5.   "description""Example project of angular 6",  
  6.   "scripts": {  
  7.     "build""tsc -p src/",  
  8.     "build:watch""tsc -p src/ -w",  
  9.     "build:e2e""tsc -p e2e/",  
  10.     "serve""lite-server -c=bs-config.json",  
  11.     "serve:e2e""lite-server -c=bs-config.e2e.json",  
  12.     "prestart""npm run build",  
  13.     "start""concurrently \"npm run build:watch\" \"npm run serve\"",  
  14.     "pree2e""npm run build:e2e",  
  15.     "e2e""concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",  
  16.     "preprotractor""webdriver-manager update",  
  17.     "protractor""protractor protractor.config.js",  
  18.     "pretest""npm run build",  
  19.     "test""concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",  
  20.     "pretest:once""npm run build",  
  21.     "test:once""karma start karma.conf.js --single-run",  
  22.     "lint""tslint ./src/**/*.ts -t verbose"  
  23.   },  
  24.   "keywords": [],  
  25.   "author""Debasis",  
  26.   "license""MIT",  
  27.   "dependencies": {  
  28.     "@angular/animations""6.0.0",  
  29.     "@angular/cli""^6.0.0",  
  30.     "@angular/common""6.0.0",  
  31.     "@angular/compiler""6.0.0",  
  32.     "@angular/core""6.0.0",  
  33.     "@angular/forms""6.0.0",  
  34.     "@angular/http""6.0.0",  
  35.     "@angular/platform-browser""6.0.0",  
  36.     "@angular/platform-browser-dynamic""6.0.0",  
  37.     "@angular/router""6.0.0",  
  38.     "@angular/service-worker""6.0.0",  
  39.     "core-js""^2.4.1",  
  40.     "crypto-js""^3.1.9-1",  
  41.     "hammer-timejs""^1.1.0",  
  42.     "hammerjs""^2.0.8",  
  43.     "lodash""^4.17.4",  
  44.     "moment""^2.20.1",  
  45.     "pepjs""^0.4.3",  
  46.     "rxjs""^6.2.0",  
  47.     "rxjs-compat""^6.2.0",  
  48.     "rxjs-system-bundle""^6.0.0",  
  49.     "systemjs""0.21.3",  
  50.     "uuid""^3.2.1",  
  51.     "zone.js""^0.8.14"  
  52.   },  
  53.   "devDependencies": {  
  54.     "@angular-devkit/build-angular""~0.6.0",  
  55.     "@angular/cli""^6.0.0",  
  56.     "@angular/compiler-cli""^6.0.0",  
  57.     "@angular/platform-server""^6.0.0",  
  58.     "@types/jasmine""~2.8.0",  
  59.     "@types/jasminewd2""^2.0.3",  
  60.     "@types/node""^6.0.45",  
  61.     "jasmine-core""3.1.0",  
  62.     "jasmine-spec-reporter""4.2.1",  
  63.     "karma""2.0.2",  
  64.     "karma-chrome-launcher""2.2.0",  
  65.     "karma-cli""~1.0.1",  
  66.     "karma-coverage""^1.1.1",  
  67.     "karma-coverage-istanbul-reporter""^1.2.1",  
  68.     "karma-jasmine""~1.1.0",  
  69.     "karma-jasmine-html-reporter""^0.2.2",  
  70.     "karma-junit-reporter""^1.2.0",  
  71.     "karma-phantomjs-launcher""^1.0.4",  
  72.     "lodash""^4.16.2",  
  73.     "protractor""5.3.1",  
  74.     "ts-node""^5.0.1",  
  75.     "tslint""^5.9.1",  
  76.     "typescript""2.7.2",  
  77.     "concurrently""^3.2.0",  
  78.     "lite-server""^2.2.2",  
  79.     "canonical-path""0.0.2",  
  80.     "rimraf""^2.5.4",  
  81.     "child-process-promise""^2.2.1",  
  82.     "codelyzer""4.3.0",  
  83.     "del""^3.0.0",  
  84.     "gulp""^3.9.1",  
  85.     "gulp-inline-ng2-template""^4.1.0",  
  86.     "node-sass""^4.8.3",  
  87.     "gulp-replace""^0.6.1",  
  88.     "gulp-sourcemaps""^2.6.4",  
  89.     "gulp-typescript""^4.0.1",  
  90.     "npm-version-up""^0.1.5",  
  91.     "phantomjs-prebuilt""^2.1.16",  
  92.     "puppeteer""^1.2.0",  
  93.     "run-sequence""^2.2.1",  
  94.     "systemjs-builder""^0.16.12"  
  95.   },  
  96.   "repository": {}  
  97. }  

systemjs.config.js File

  1. /** 
  2.  * System configuration for Angular samples 
  3.  * Adjust as necessary for your application needs. 
  4.  */  
  5. (function (global) {  
  6.     System.config({  
  7.         paths: {  
  8.             // paths serve as alias  
  9.             'npm:''/node_modules/',  
  10.         },  
  11.         // map tells the System loader where to look for things  
  12.         map: {  
  13.             // our app is within the app folder  
  14.             'app''.',  
  15.             '@angular/core''npm:@angular/core/bundles/core.umd.min.js',  
  16.             '@angular/common''npm:@angular/common/bundles/common.umd.min.js',  
  17.             '@angular/compiler''npm:@angular/compiler/bundles/compiler.umd.min.js',  
  18.             '@angular/forms''npm:@angular/forms/bundles/forms.umd.min.js',  
  19.             '@angular/router''npm:@angular/router/bundles/router.umd.min.js',  
  20.             '@angular/http''npm:@angular/http/bundles/http.umd.min.js',  
  21.             '@angular/platform-browser''npm:@angular/platform-browser/bundles/platform-browser.umd.min.js',  
  22.             '@angular/platform-browser/animations''npm:@angular/platform-browser/bundles/platform-browser-animations.umd.min.js',  
  23.             '@angular/platform-browser-dynamic''npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.min.js',  
  24.             '@angular/service-worker''npm:@angular/service-worker/bundles/service-worker.umd.min.js',  
  25.             '@angular/animations''npm:@angular/animations/bundles/animations.umd.min.js',  
  26.             '@angular/animations/browser''npm:@angular/animations/bundles/animations-browser.umd.min.js',  
  27.             // other libraries  
  28.             'rxjs''npm:rxjs',  
  29.             'rxjs-compat''npm:rxjs-compat',  
  30.             'rxjs-system-bundle''npm:rxjs-system-bundle',  
  31.             'lodash''npm:lodash/index.js',  
  32.             'uuid''npm:uuid/index.js',  
  33.             'crypto''npm:crypto-js/crypto-js.js',  
  34.             'oidc-client''npm:oidc-client/lib/oidc-client.js',  
  35.             'tslib''npm:tslib/tslib.js',  
  36.             'hammerjs''npm:hammerjs/hammer.js',  
  37.             'hammer-timejs''npm:hammer-timejs/hammer-time.js',  
  38.             'moment''npm:moment/moment.js',  
  39.             'core-js''npm:core-js',  
  40.             'zone.js''npm:zone.js',  
  41.         },  
  42.         // packages tells the System loader how to load when no filename and/or no extension  
  43.         packages: {  
  44.             app: { main: 'main.js', defaultExtension: 'js' },  
  45.             app: { defaultExtension: 'js', },  
  46.             rxjs: { defaultExtension: 'js', main: "index.js" },  
  47.             "rxjs-compat": { defaultExtension: 'js', main: "index.js" },  
  48.             "rxjs/operators": { "main""index.js""defaultExtension""js" },  
  49.             "rxjs/internal-compatibility": { "main""index.js""defaultExtension""js" },  
  50.             "rxjs/testing": { "main""index.js""defaultExtension""js" },  
  51.             'rxjs/ajax': { main: 'index.js', defaultExtension: 'js' },  
  52.             'rxjs/webSocket': { main: 'index.js', defaultExtension: 'js' },  
  53.             '..': { defaultExtension: 'js' }  
  54.         }  
  55.     });  
  56. })(this);  

Sample code for app.component.welcome.ts file -

  1. import { Component } from "@angular/core";  
  2. @Component({  
  3.     selector: "welcome-prog",  
  4.     template: `<h1>First Programe in Angular 6.0. Welcome to Angular World</h1>   
  5.                 <br>  
  6.                 <a  href="../index.html" style="color:red;">  
  7.                     <h3>Return to Home</h3>  
  8.                 </a>  
  9.                 `  
  10. })  
  11. export class WelcomeComponent {  
  12.     constructor() {  
  13.     }  
  14. }  
Sample code for app.module.demo.ts -
  1. import { NgModule, NO_ERRORS_SCHEMA, } from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3. import { FormsModule } from '@angular/forms';  
  4. import { ReactiveFormsModule } from "@angular/forms";  
  5. import { HttpModule } from '@angular/http';  
  6. import { WelcomeComponent } from './day1/app.component.welcome';  
  7. @NgModule({  
  8.     imports: [BrowserModule, FormsModule, ReactiveFormsModule, HttpModule],  
  9.     declarations: [WelcomeComponent  
  10. ],  
  11.     bootstrap: [WelcomeComponent],  
  12.     schemas: [NO_ERRORS_SCHEMA]  
  13. })  
  14. export class DemoModule { }  
Sample code for main.ts file -
  1. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';  
  2. import { DemoModule } from './app.module.demo';  
  3. platformBrowserDynamic().bootstrapModule(DemoModule);  
Sample code foe index.html file -
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <!--<base href="/">-->  
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6.     <meta charset="utf-8">  
  7.     <title>Angular 6 - Console</title>  
  8.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  9.     <meta name="description" content="">  
  10.     <meta name="keywords" content="">  
  11.     <meta name="author" content="">  
  12.     <link href="../resource/css/bootstrap.min.css" rel="stylesheet" type="text/css">  
  13.     <link rel="stylesheet" href="../resource/css/font-awesome.min.css" type="text/css">  
  14.     <link rel="stylesheet" href="../resource/css/jquery-ui.css" type="text/css">  
  15.     <link href="../resource/css/style.css" rel="stylesheet" type="text/css">  
  16.     <link rel="shortcut icon" href="../resource/img/favicon/favicon.ico">  
  17.     <link href="../resource/css/app.css" rel="stylesheet"  type="text/css"/>  
  18. </head>  
  19. <body>  
  20.     <div class="content">  
  21.         <welcome-prog></welcome>  
  22.     </div>          
  23.     <script src="../node_modules/core-js/client/shim.min.js" type="text/javascript"></script>  
  24.     <script src="../node_modules/zone.js/dist/zone.js" type="text/javascript"></script>  
  25.     <script src="../node_modules/systemjs/dist/system.src.js" type="text/javascript"></script>  
  26.     <script src="systemjs.config.js" type="text/javascript"></script>  
  27.     <script>  
  28.         System.import('../main.js').catch(function (err) { console.error(err); });  
  29.     </script>  
  30. </body>  
  31. </html>  
Now, run the above code in the browser and the output will be like below.
 
 


Similar Articles