Angular 6 – Overview ”; Previous Next There are five major releases of Angular. The first version that was released is Angular 1, which is also called AngularJS. Angular 1 was followed by Angular 2, which came in with a lot of changes when compared to Angular 1. The structure of Angular is based on the components/services architecture. AngularJS was based on the model view controller. Angular 6 released in May 2018 proves to be a major breakthrough and is the latest release from the Angular team after Angular 5. Angular 6 is almost the same as Angular 5. It has a backward compatibility with Angular 5. Projects developed in Angular 5 will work without any issues with Angular 5. Let us now see the new features and the changes made in Angular 5. Angular 5 and its Features Angular 5 was released in Nov 2017. As per its goal of speed and size, It was way faster and of smaller size than that of Angular 4. Following are the features that were introduced in Angular 5. HTTPClient API − HTTPClient API was introduced to deprecate the HTTP library. HTTPClient API is much faster, secure and efficient than HTTP library. Multiple export aliases − A component can be exported using multiple aliases to ease the migration process. Internationalized Pipes for Number, Date, and Currency − New pipes are introduced for better standardization. Lambda support − lambda expressions with proper names can be used instead of functions. Build Optimizer – Build Optimizer introduced. It optimizes the build size and improves the application speed. Angular CLI uses Build Optimizer automatically. Improved Compiler − Compiler from Angular 5 onwards supports incremental compilation leading for faster compilation. Compiler uses TypeScript transforms, a new feature of TypeScript 2.3 available onwards. Let us now see the new features added to Angular 6 − Updated Angular CLI, Command Line interface − New commands added, like ng-update to migrate from previous version to current version. ng-add to quickly add application features to make application a progressive web apps. Updated CDK, Component Development Kit − Supports creating custom UI elements without need of angular material library. Supports responsive web design layouts. Supports overlay packages to create pop-ups. Updated Angular Material − New Tree component added, mat-tree, a styled version and cdk-tree, a unstyled version, to represent a hierarchical structure like tree. Usage of RxJS, a reactive JS library Angular Element − Allows Angular Components to be published as Web Components which can then be used in any HTML page. Using Angular Element package, native custom elements can be created easily. Multiple Validators − Allows multiple validators to be applicable on a form builder. Tree Shaking on Services − Now tree shaking can be applied on services as well to remove the dead code. Print Page Previous Next Advertisements ”;
Category: angular6
Angular 6 – Services
Angular 6 – Services ”; Previous Next In this chapter, we will discuss the services in Angular 6. We might come across a situation where we need some code to be used everywhere on the page. It can be for data connection that needs to be shared across components, etc. Services help us achieve that. With services, we can access methods and properties across other components in the entire project. To create a service, we need to make use of the command line. The command for the same is − C:projectA6Angular6App>ng g service myservice CREATE src/app/myservice.service.spec.ts (392 bytes) CREATE src/app/myservice.service.ts (138 bytes) The files are created in the app folder as follows − Following are the files created at the bottom – myservice.service.specs.ts and myservice.service.ts. myservice.service.ts import { Injectable } from ”@angular/core”; @Injectable() export class MyserviceService { constructor() { } } Here, the Injectable module is imported from the @angular/core. It contains the @Injectable method and a class called MyserviceService. We will create our service function in this class. Before creating a new service, we need to include the service created in the main parent app.module.ts. import { BrowserModule } from ”@angular/platform-browser”; import { NgModule } from ”@angular/core”; import { RouterModule} from ”@angular/router”; import { AppComponent } from ”./app.component”; import { MyserviceService } from ”./myservice.service”; import { NewCmpComponent } from ”./new-cmp/new-cmp.component”; import { ChangeTextDirective } from ”./change-text.directive”; import { SqrtPipe } from ”./app.sqrt”; @NgModule({ declarations: [ SqrtPipe, AppComponent, NewCmpComponent, ChangeTextDirective ], imports: [ BrowserModule, RouterModule.forRoot([ { path: ”new-cmp”, component: NewCmpComponent } ]) ], providers: [MyserviceService], bootstrap: [AppComponent] }) export class AppModule { } We have imported the Service with the class name and the same class is used in the providers. Let us now switch back to the service class and create a service function. In the service class, we will create a function which will display todays date. We can use the same function in the main parent component app.component.ts and also in the new component new-cmp.component.ts that we created in the previous chapter. Let us now see how the function looks in the service and how to use it in components. import { Injectable } from ”@angular/core”; @Injectable() export class MyserviceService { constructor() { } showTodayDate() { let ndate = new Date(); return ndate; } } In the above service file, we have created a function showTodayDate. Now we will return the new Date () created. Let us see how we can access this function in the component class. app.component.ts import { Component } from ”@angular/core”; import { MyserviceService } from ”./myservice.service”; @Component({ selector: ”app-root”, templateUrl: ”./app.component.html”, styleUrls: [”./app.component.css”] }) export class AppComponent { title = ”Angular 6 Project!”; todaydate; constructor(private myservice: MyserviceService) {} ngOnInit() { this.todaydate = this.myservice.showTodayDate(); } } The ngOnInit function gets called by default in any component created. The date is fetched from the service as shown above. To fetch more details of the service, we need to first include the service in the component ts file. We will display the date in the .html file as shown below − {{todaydate}} <app-new-cmp></app-new-cmp> // data to be displayed to user from the new component class. Let us now see how to use the service in the new component created. import { Component, OnInit } from ”@angular/core”; import { MyserviceService } from ”./../myservice.service”; @Component({ selector: ”app-new-cmp”, templateUrl: ”./new-cmp.component.html”, styleUrls: [”./new-cmp.component.css”] }) export class NewCmpComponent implements OnInit { todaydate; newcomponent = “Entered in new component created”; constructor(private myservice: MyserviceService) {} ngOnInit() { this.todaydate = this.myservice.showTodayDate(); } } In the new component that we have created, we need to first import the service that we want and access the methods and properties of the same. Please see the code highlighted. The todaydate is displayed in the component html as follows − <p> {{newcomponent}} </p> <p> Today”s Date : {{todaydate}} </p> The selector of the new component is used in the app.component.html file. The contents from the above html file will be displayed in the browser as shown below − If you change the property of the service in any component, the same is changed in other components too. Let us now see how this works. We will define one variable in the service and use it in the parent and the new component. We will again change the property in the parent component and will see if the same is changed in the new component or not. In myservice.service.ts, we have created a property and used the same in other parent and new component. import { Injectable } from ”@angular/core”; @Injectable() export class MyserviceService { serviceproperty = “Service Created”; constructor() { } showTodayDate() { let ndate = new Date(); return ndate; } } Let us now use the serviceproperty variable in other components. In app.component.ts, we are accessing the variable as follows − import { Component } from ”@angular/core”; import { MyserviceService } from ”./myservice.service”; @Component({ selector: ”app-root”, templateUrl: ”./app.component.html”, styleUrls: [”./app.component.css”] }) export class AppComponent { title = ”Angular 4 Project!”; todaydate; componentproperty; constructor(private myservice: MyserviceService) {} ngOnInit() { this.todaydate = this.myservice.showTodayDate(); console.log(this.myservice.serviceproperty); this.myservice.serviceproperty = “component created”; // value is changed. this.componentproperty = this.myservice.serviceproperty; } } We will now fetch the variable and work on the console.log. In the next line, we will change the value of the variable to “component created”. We will do the same in new-cmp.component.ts. import { Component, OnInit } from ”@angular/core”; import { MyserviceService } from ”./../myservice.service”; @Component({ selector: ”app-new-cmp”, templateUrl: ”./new-cmp.component.html”, styleUrls: [”./new-cmp.component.css”] }) export class NewCmpComponent implements OnInit { todaydate; newcomponentproperty; newcomponent = “Entered in newcomponent”; constructor(private myservice: MyserviceService) {} ngOnInit() { this.todaydate = this.myservice.showTodayDate(); this.newcomponentproperty = this.myservice.serviceproperty; } } In the above component, we are not changing anything but directly assigning the property to the component property. Now when you execute it in the browser, the service property will be changed since the value of it is changed in app.component.ts and the same will be displayed for the new-cmp.component.ts. Also check the value in the console before it is changed. Print Page