In today's’ web development world, Angular is known as one of the most important and used frameworks. Recently, Angular 6.0 version has been launched. So, in this document, we will discuss how to develop the first component or program in Angular 6.0.
The Angular framework is an open source JavaScript-based framework. It was actually developed by Misko Havery and Adam Abrons in 2009. This framework supports the JavaScript-based MVC (MVVM) framework. As per the Google, the definition of the angular is a 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 from their projects are –
To start development in Angular 6.0, we need to follow the below perquisites:
So, before starting with Angular 6.0, we first need to know how to install the TypeScript tool. To install TypeScript, we first need to install Node.js. The latest version of Node.js can be downloaded from the below URL:
To install TypeScript, we can download the latest version of TypeScript by either using the command line argument in Node.js or using Visual Studio to directly download it from the NuGet Package Manager.
Command line prompt to install TypeScript:
npm install -g typescript
Command line prompt for install Lite-Server:
npm install -g lite-server
Now, we will discuss how to set up the environment for an Angular 6 project. In this post, we will develop our first program in Angular 6. Before getting started, let’s discuss the project configuration files which are required in Angular. An Angular project always contains 3 major configuration files. They are:
The Angular framework is basically a component-based framework (from Angular 2.0 or above). SO, we first need to understand what components are and how they can be defined. In Angular, a component is just like a class in other OOP-based languages. This class is basically defined to display any element on the application screen. So, as per our requirements, we can create, update, or delete any existing component in the application. In TypeScript, we can define the component class with the help of the @Compoent()
decorator.
The @Component
decorator actually used to decorate 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 values to properties to fix the behavior of the components. The most used properties are given below:
styleUrls
attribute.
As we already discussed, everything in Angular 6 belongs to an Angular Module. So, for developing the root component, we first need to declare our Angular module. The 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 as follows:
import { NgModule } from '@angular/core';
@NgModule()
export class SampleModule { }
Finally, we reach a position where we need to create our first component using Angular 6. It can be done by creating a class decorated with @Component decorator which defined within the “@angular/core” library. Below the sample code for the angular component –
import { Component } from "@angular/core";
@Component({
selector: "welcome-prog",
template: `<h1>First Program in Angular. Welcome to Angular World</h1>
<br>
<a href="../index.html" style="color:red;">
<h3>Return to Home</h3>
</a>
`
})
export class WelcomeComponent {
constructor() {
}
}
Now, the next step is to add the component within the angular module. It can be done using “declarations” option within “NgModule” decorator. For adding the component, we need to import the component within the module by using import keyword.
import { NgModule, NO_ERRORS_SCHEMA, } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from "@angular/forms";
import { HttpModule } from '@angular/http';
import { WelcomeComponent } from './day1/app.component.welcome';
@NgModule({
imports: [BrowserModule, FormsModule, ReactiveFormsModule, HttpModule],
declarations: [ WelcomeComponent ],
bootstrap: [WelcomeComponent],
schemas: [NO_ERRORS_SCHEMA]
})
export class DemoModule { }
As we already discussed that a single angular application can contain more than one angular modules. But out of the all module, only one module can be bootstrapped at the beginning. In Angular 6 this bootstrapping process needs to be done manually with the help of “platformBrowserDynamic” function which is defined within the “@angular/platform-browser-dynamic” library.
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { DemoModule } from './app.module.demo';
platformBrowserDynamic().bootstrapModule(DemoModule);
Code of Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>Angular 6 - Console</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<link href="../resource/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../resource/css/font-awesome.min.css" type="text/css">
<link rel="stylesheet" href="../resource/css/jquery-ui.css" type="text/css">
<link href="../resource/css/style.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="../resource/img/favicon/favicon.ico">
<link href="../resource/css/app.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="content">
<home></home>
</div>
<script>
sessionStorage.setItem("dayno", JSON.stringify(1));
</script>
<script src="../node_modules/core-js/client/shim.min.js" type="text/javascript"></script>
<script src="../node_modules/zone.js/dist/zone.js" type="text/javascript"></script>
<script src="../node_modules/systemjs/dist/system.src.js" type="text/javascript"></script>
<script src="systemjs.config.js" type="text/javascript"></script>
<script>
System.import('../main.js').catch(function (err) { console.error(err); });
</script>
</body>
</html>
Now after executing the above code, the output will be as below -
if anyone wants to access the complete source code of this article, they can be downloaded from the below URLs -
Hope you found this information helpful.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)