Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Started With Angular 6.0

DZone's Guide to

Getting Started With Angular 6.0

The latest version of Angular is here! Let's take a look at the great new features the latest version of the framework has to offer, plus a tutorial to get you started.

· Web Dev Zone ·
Free Resource

Code something amazing with the IBM library of open source blockchain patterns. Content provided by IBM.

Angular has released its latest version, Angular 6.0. In this article, we will look at the new features of Angular 6.0 and also set up a new project with the help of Angular CLI 6.0 and Visual Studio Code.

What's New in Angular 6.0?

  • ng update: A new CLI command that will update your project dependencies to their latest versions.
  • ng add: Another new CLI command that will make adding new capabilities to your project easier.
  • Angular Elements: This is a new feature that allows us to compile Angular components to native web components which we can use in our Angular app.
  • <template> element is deprecated: You can't use <template> anymore inside of your component templates. You need to use <ng-template> instead.
  • Angular CLI now has support for creating and building libraries. To create a library project within your CLI workspace, run the following command:
    • ng generate library <name>
    • For example:ng generate library my-demo-lib  
  • Angular Material Starter Components: If you run ng add @angular/material to add material to an existing application, you will also be able to generate three new starter components.
    1. Material Sideman A starter component including a toolbar with the app name and the side navigation.
    2. Material Dashboard A starter dashboard component containing a dynamic grid list of cards.
    3. Material Data Table A starter data table component that is pre-configured with a datasource for sorting and pagination.
  • Angular CLI now has support for workspaces containing multiple projects, such as multiple applications and/or libraries.
  • The ".angular-cli.json" file has been deprecated. Angular projects will now use "angular.json" instead of ".angular-cli.json" for build and project configuration.
  • Angular 6 will also allow us to use RxJS V6 with our application.
  • Tree Shakable Providers: Angular 6.0 allows us to bundle services into the code base in modules where they are injected. This will help us to make our application smaller.

For example, in previous Angular versions, we used to reference our services as below:

// In app.module.ts  

@NgModule({  
  ...  
  providers: [MyService]  
})  
export class AppModule {}  

// In myservice.ts   

import { Injectable } from '@angular/core';  

@Injectable()  
export class MyService {  
  constructor() { }  
}  

This approach will still work, but Angular 6.0 provides a new and easier alternative to this. We no longer need to add references in our NgModule. We can inject the reference directly into the service. Therefore, we can use the service as below:

// In myservice.ts  

import { Injectable } from '@angular/core';  

@Injectable({  
  providedIn: 'root',  
})  
export class MyService {  
  constructor() { }  
}

That is all about the features/improvements in the latest release of Angular. Let us move on and create our first application using Angular 6.0.

Prerequisites

  • Install the latest version of Node.js from here.
  • Install Visual Studio Code from here.

Installing Node.js will also install npm on your machine. After installing Node.js, open the command prompt and run the following set of commands to check the version of Node and npm installed on your machine.

Refer to the below image:

Installing Angular CLI

Since node and npm are installed, the next step is to install Angular CLI. Run the following command in a command window.

  •  npm i -g @angular/cli 

This will install Angular 6.0 CLI globally on your machine.

Create the Angular 6 App

Open VS Code and navigate to View >> Integrated Terminal.

This will open a terminal window in VS Code.

Type the following sequence of commands in the terminal window. These commands will create a directory with the name "ng6Demo" and then create an Angular application with the name "ng6App" inside that directory.

  • mkdir ng6Demo 
  • cd ng6Demo 
  • ng new ng6App 

Hence, we have created our first Angular 6 application using VS Code and Angular CLI. Now, run the following command to open the project.

  • code .

This will open the code file of our application in a new VS Code window. You can see the following file structure in Solution Explorer.

Notice that the folder structure is a little bit different from the older version of Angular. We have a new "angular.json" file instead of the old ".angular-cli.json" file. This config file will still serve the same task as before but the schema has changed a bit.

Open package.json file and you can observe that we have the latest Angular 6.0.0 packages installed in our project.

{  
  "name": "ng6-app",  
  "version": "0.0.0",  
  "scripts": {  
    "ng": "ng",  
    "start": "ng serve",  
    "build": "ng build",  
    "test": "ng test",  
    "lint": "ng lint",  
    "e2e": "ng e2e"  
  },  
  "private": true,  
  "dependencies": {  
    "@angular/animations": "^6.0.0",  
    "@angular/common": "^6.0.0",  
    "@angular/compiler": "^6.0.0",  
    "@angular/core": "^6.0.0",  
    "@angular/forms": "^6.0.0",  
    "@angular/http": "^6.0.0",  
    "@angular/platform-browser": "^6.0.0",  
    "@angular/platform-browser-dynamic": "^6.0.0",  
    "@angular/router": "^6.0.0",  
    "core-js": "^2.5.4",  
    "rxjs": "^6.0.0",  
    "zone.js": "^0.8.26"  
  },  
  "devDependencies": {  
    "@angular/compiler-cli": "^6.0.0",  
    "@angular-devkit/build-angular": "~0.6.0",  
    "typescript": "~2.7.2",  
    "@angular/cli": "~6.0.0",  
    "@angular/language-service": "^6.0.0",  
    "@types/jasmine": "~2.8.6",  
    "@types/jasminewd2": "~2.0.3",  
    "@types/node": "~8.9.4",  
    "codelyzer": "~4.2.1",  
    "jasmine-core": "~2.99.1",  
    "jasmine-spec-reporter": "~4.2.1",  
    "karma": "~1.7.1",  
    "karma-chrome-launcher": "~2.2.0",  
    "karma-coverage-istanbul-reporter": "~1.4.2",  
    "karma-jasmine": "~1.1.1",  
    "karma-jasmine-html-reporter": "^0.2.2",  
    "protractor": "~5.3.0",  
    "ts-node": "~5.0.1",  
    "tslint": "~5.9.1"  
  }  
}

Execution Demo

The name of our Angular application is ng6app which is inside the ng6demo directory.

So, we will first navigate to our application using the below commands.

  • cd ng6demo 

  • cd ng6app 

Now we use the following command to start the web server: 

  • ng serve

After running this command, you can see that it is asking to open http://localhost:4200 in your browser. So, open any browser on your machine and navigate to this URL. Now, you can see the following page.

Now we will try to change the welcome text on the screen. Navigate to the /src/app/app.components.ts file, and replace the code with the code below: 

import { Component } from '@angular/core';  

@Component({  
  selector: 'app-root',  
  templateUrl: './app.component.html',  
  styleUrls: ['./app.component.css']  
})  
export class AppComponent {  
  title = 'Csharp Corner';  
}

Now open the browser. You can see that the web page has been updated with a new welcome message: "Welcome to Csharp Corner!"

Conclusion

In this article we learned about the new features of Angular 6.0. We have installed the Angular 6.0 CLI and created our first Angular 6.0 application with the help of Visual Studio Code. We have also customized the welcome message on the webpage.

You can check my other articles on Angular here.

Start coding something amazing with our library of open source Cloud code patterns. Content provided by IBM.

Topics:
web dev ,angular 6 ,web application development ,angular ,development frameworks

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}