Let’s dive into Angular 4!

Angular 4 is now available! Actually Angular uses semver since Angular 2, which requires the major number being increased when breaking changes were introduced. The Angular team postponed features that cause breaking changes, which will be released with Angular 4. Angular 3 was skipped to be able to align the version numbers of the core modules, because the Router already had version 3.

As per the Angular team, Angular 4 applications are going to be less space consuming and faster than before. They have separated animation package from @angular/core package. If anybody is not using animation package so extra space of code will not end up in the production. The template binding syntax now supports if/else style syntax. Angular 4 is now compatible with most recent version of Typescript 2.1 and 2.2. So, Angular 4 is going to be more exciting.

Now I’ll show you how to do setup of Angular 4 in your project.

Let’s start Angular setup with three different ways:

  • You can use Angular-CLI  (Command Line Interface) , It will install all dependencies for you.
  • You can migrate from Angular 2 to Angular 4.
  • You can use github and clone the Angular4-boilerplate. (It is the easiest one. ?)

Angular Setup using Angular-CLI(command Line Interface).

Before You start using Angular-CLI , make sure You have node installed in your machine. Here, I am using node v7.8.0. Now, Open your terminal and type the following command for Angular-CLI.

npm install -g @angular/cli

Just for the confirmation, try with ng -v and you’ll get something like below:

<img class=”alignnone wp-image-2160″ src=”https://secureservercdn.net/160.153.138.177/tzj.167.myftpupload.com/wp-content/uploads/2017/05/Screen-Shot-2017-05-05-at-11.13.25-AM-300×98.png” alt=”” width=”527″ height=”172″ />

Let’s install Angular 4 using Angular-CLI.

ng new Angular4-boilerplate

cd Angular4-boilerplate 

We are all set for Angular 4. Its pretty easy and straightforward method.?

Angular Setup by migrating from Angular 2 to Angular 4

Now Let’s see the second approach. I ll show you how to migrate Angular 2 to Angular 4. For that You need clone any Angular 2 project and update Angular 2 dependencies with the Angular 4 Dependency in your package.json as following:

 

 "dependencies": {
    "@angular/animations": "^4.1.0",
    "@angular/common": "4.0.2",
    "@angular/compiler": "4.0.2",
    "@angular/core": "^4.0.1",
    "@angular/forms": "4.0.2",
    "@angular/http": "4.0.2",
    "@angular/material": "^2.0.0-beta.3",
    "@angular/platform-browser": "4.0.2",
    "@angular/platform-browser-dynamic": "4.0.2",
    "@angular/router": "4.0.2",
    "typescript": "2.2.2"
   }

These are the main dependencies for Angular 4. Now You can npm install and then npm start to run the application. For reference my package.json.

Angular setup from github project

Before starting this step make sure you have git installed in your machine. Open your terminal and clone the angular4-boilerplate using below command:

git@github.com:CypherTree/angular4-boilerplate.git

Then install all dependencies and run it.

npm install

npm start

And you are done with the Angular 4 setup. All the steps are very straightforward so you can opt any of them.

Directory Structure of the angular4-boilerplate

Angular4-boilerplate
-karma
-node_modules
-src
   -mocks
   -models
      -loginform.ts
      -index.ts
   -modules
      -app
        -app.component.ts
     -app.component.html
      -login
    -login.component.ts
    -login.component.html
    -login.component.css
      -widget
     -widget.component.ts
     -widget.component.html
     -widget.component.css
     . . . . . .    
   -services
       -login.service.ts
    -rest.service.ts
   -app.routing.module.ts
   -app.module.ts
   -bootstrap.ts
   -index.html
   -vendor.ts
-typings
-webpack
-package.json
-tsconfig.json
-tslint.json
-typings.json        

Basic understanding for Directory structure:

  • All the code resides in src folder. mocks folder is for mock data that is used in testing purpose.
  • model folder contains the class and interface that used in component.
  • modules folder contains list of components such as app, login, widget etc. All component contains typescript,html and css file. index.ts is for exporting all the class.
  • services folder contains list of services used in application. I have seperated rest service and different component service. In rest service contains different http methods. Login service works as mediator between login component and rest service.
  • app.routing.ts file describes all possible routes for the application.
  • app.module.ts describes app module as root component.
  • bootstrap.ts will run the whole application.
  • webpack folder contains webpack configuration file.
  • package.json file is for all list of dependencies.
  • karma contains karma configuration for unit test.
  • node_modules contains list of package bundles.

Lets start with Login component. In login.component.html

<form #loginForm="ngForm">
   <md-card layout-align="center center">
       <md-card-title> Dreamfactory - Addressbook 2.0 </md-card-title>
       <md-divider></md-divider>
       <md-card-content layout-align="center center">
           <md-input-container>
               <label>Email</label>
               <input mdInput type="email" [(ngModel)]="form.email" id="email" name="email">
           </md-input-container>
           <md-input-container>
               <label>Password</label>
               <input mdInput type="password" [(ngModel)]="form.password" id="password" name="password">
           </md-input-container>
       </md-card-content>
       <md-card-actions layout="row" layout-align="center center">
           <button md-raised-button (click)="getLogin(form)" color="accent">Login</button>
       </md-card-actions>
   </md-card>
</form>

In login.component.ts

import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { Form, FormGroup } from '@angular/forms';
import { LoginForm } from '../../models';
import { LoginService } from '../../services/login.service';

@Component({
    selector: 'login',
    template: require('./login.component.html'),
    styles: [require('./login.component.css')]
})
export class LoginComponent {
    constructor(private loginService: LoginService, private router: Router, form: LoginForm) { }
    getLogin(form: LoginForm): void {
      let username = form.email;
      let password = form.password;
      this.loginService.getAuthenticate(form).subscribe(() => {
          this.router.navigate(['/calender']);
      });
    }
}

We need to export this component to in index.ts.

export * from './login/login.component';

In root component, app.module.ts  file you just need to import that component.

. . . 
import { LoginComponent } from './modules';
. . .

@NgModule({
    bootstrap: [AppComponent],
    declarations: [
       LoginComponent
       . . . 
       . . . 
       ]
    . . .
  })
  export class AppModule { }

and after that npm install and npm start. Here, you go! You can check login screen in your localhost. In case of any difficulty, You can refer the angular4-boilerplate.

Basically I can feel less building package and more faster response with Angular 4 application and Although I found Exactly similar to Angular 2 in coding.

Bhoomi Bhalani, UI Developer, Cyphertree Technologies Pvt Ltd.



Leave a Reply