Category: 02. Angular 7

https://cdn3d.iconscout.com/3d/free/thumb/free-angularjs-3d-icon-download-in-png-blend-fbx-gltf-file-formats–google-logo-html-angular-coding-lang-pack-logos-icons-7577994.png?f=webp

  • Angular 7 Project Setup (Create first app)

    Following are the Angular CLI commands to create the first Angular app.

    npm install -g @angular/cli  
    
    ng new my-dream-app  
    
    cd my-dream-app  
    
    ng serve 

      Run the following command to create your first Angular app.

      ng new my-first-app  
      Angular 7 Project Setup (Create first app)

      Navigate to your first app.

      cd my-first-app  

      Start your server to run app.

      ng serve  
      Angular 7 Project Setup (Create first app)

      Your server is running on localhost:4200. Now, go to the browser and open it.

      Angular 7 Project Setup (Create first app)

      Now, you need an IDE to edit and run your app’s code. Here, we are using WebStorm.

      Open WebStorm and open your app “my-first-app” in the IDE. It will look like this:

      Angular 7 Project Setup (Create first app)

      Here, go to src folder, you will see app folder there. Expand the app folder.

      Angular 7 Project Setup (Create first app)

      You will see 5 components there:

      • app.component.css
      • app.component.html
      • app.component.spec.ts
      • app.component.ts
      • app.module.ts

      You can see the code within the different components to understand what is going on and which part is responsible for the outlook of the app.

      app.component.css

      This part is empty because we don’t specify any CSS here.

      Angular 7 Project Setup (Create first app)

      app.component.html

      This is the most important component, the front page of your app. Here, you can change the salutation used before your app’s name. You can also change the content on the front page and their respective links.

      Angular 7 Project Setup (Create first app)

      Code:

        <div style="text-align:center">  
      
        <h1>  
      
          Welcome to {{ title }}!  
      
        </h1>  
      
        <img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">  
      
      </div>  
      
      <h2>Here are some links to help you start: </h2>  
      
      <ul>  
      
        <li>  
      
          <h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>  
      
        </li>  
      
        <li>  
      
          <h2><a target="_blank" rel="noopener" href="https://angular.io/cli">CLI Documentation</a></h2>  
      
        </li>  
      
        <li>  
      
          <h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>  
      
        </li>  
      
      </ul>

        app.component.spec.ts:

        This file is used for testing purpose only.

        import { TestBed, async } from '@angular/core/testing';  
        
        import { AppComponent } from './app.component';  
        
          
        
        describe('AppComponent', () => {  
        
          beforeEach(async(() => {  
        
            TestBed.configureTestingModule({  
        
              declarations: [  
        
                AppComponent  
        
              ],  
        
            }).compileComponents();  
        
          }));  
        
          
        
          it('should create the app', () => {  
        
            const fixture = TestBed.createComponent(AppComponent);  
        
            const app = fixture.debugElement.componentInstance;  
        
            expect(app).toBeTruthy();  
        
          });  
        
          
        
          it(should have as title 'my-first-app', () => {  
        
            const fixture = TestBed.createComponent(AppComponent);  
        
            const app = fixture.debugElement.componentInstance;  
        
            expect(app.title).toEqual('my-first-app');  
        
          });  
        
          
        
          it('should render title in a h1 tag', () => {  
        
            const fixture = TestBed.createComponent(AppComponent);  
        
            fixture.detectChanges();  
        
            const compiled = fixture.debugElement.nativeElement;  
        
            expect(compiled.querySelector('h1').textContent).toContain('Welcome to my-first-app!');  
        
          });  
        
        });  

          app.component.ts

          You can change the name of your app here. You just have to change the title.

          import { Component } from '@angular/core';  
          
            
          
          @Component({  
          
            selector: 'app-root',  
          
            templateUrl: './app.component.html',  
          
            styleUrls: ['./app.component.css']  
          
          })  
          
          export class AppComponent {  
          
            title = 'my-first-app';  
          
          }

          app.module.ts

          import { BrowserModule } from '@angular/platform-browser';  
          
          import { NgModule } from '@angular/core';  
          
            
          
          import { AppComponent } from './app.component';  
          
            
          
          @NgModule({  
          
            declarations: [  
          
              AppComponent  
          
            ],  
          
            imports: [  
          
              BrowserModule  
          
            ],  
          
            providers: [],  
          
            bootstrap: [AppComponent]  
          
          })  
          
          export class AppModule { }
        1. History and different versions of Angular

          The first version of Angular was Angular1.0 (also known as AngularJS) which was released in 2010. But here, we are talking about Angular so; let’s see history and different versions of Angular.

          Angular2

          Angular 2.0 was first introduced in October 2014. It was a complete rewrite of Angular so, the drastic changes in the 2.0 version created controversy among developers. On April 30, 2015, the Angular developers announced that Angular 2 moved from Alpha to Developer Preview and then Beta version was released in December 2015. Its first version was published in May 2016 and the final version was released on September 14, 2016.

          Angular4

          Angular 4 version was announced on 13 December 2016. The developers skipped the version 3 due to some confusion. Its final version was released on 23 March 2017.

          This version has some additional features:

          • This version introduced HttpClient, a smaller, easier to use, and more powerful library for making HTTP Requests.
          • It provides new router life cycle events for Guards and Resolvers. Four new events: GuardsCheckStart, GuardsCheckEnd, ResolveStart, ResolveEnd join the existing set of life cycle event such as NavigationStart.
          • It provides the support of conditionally disable animations.

          Angular5

          This version was released on 1 Nov, 2017. It provided some improvements to support for progressive web apps, also provides improvements related to Material Design.

          Angular6

          This version was released on 4 may, 2018. It was a major relaese which provides some features like: ng update, ng add, Angular Elements, Angular Material + CDK Components, Angular Material Starter Components, CLI Workspaces, Library Support, Tree Shakable Providers, Animations Performance Improvements, and RxJS v6.

          Angular 7

          The latest version of Angular is Angular 7. It was released on October 18, 2018. It consists of many extensive features:

          • Updates regarding Application Performance
          • Angular Material & CDK
          • Virtual Scrolling
          • Improved Accessibility of Selects
          • Supports Content Projection using web standard for custom elements
          • Dependency updates regarding TypeScript 3.1, RxJS 6.3, Node 10
        2. How to install Angular 7?

          Angular 7 Environment Setup

          In this page, you will see how you can install the prerequisites needed to run your first Angular 7 app.

          Install Visual Studio Code IDE or JetBrains WebStorm

          You must have an IDE like Visual Studio Code IDE or JetBrains WebStorm to run your Angular 7 app.

          VS Code is light and easy to setup, it has a great range of built-in code editing, formatting, and refactoring features. It is free to use. It also provides a huge number of extensions that will significantly increase your productivity.

          You can download VS Code from here: https://code.visualstudio.com

          JetBrains WebStorm is also a great IDE to develop Angular 7 apps. It is fast, attractive, and very easy to use software but, it is not free to use. You have to purchase it later, it only provides a trial period of 30 days for free.

          You can download VS Code from here: https://www.jetbrains.com/webstorm/download/#section=windows

          We are using JetBrains WebStorm in this tutorial.

          Install Node.js

          You should install node.js to run your Angular 7 app. It manages npm dependencies support some browsers when loading particular pages. It provides required libraries to run Angular project. Node.js serves your run-time environment as your localhost.

          See how to install node.js: install-nodejs

          Or

          Just go to node.js official website https://nodejs.org/en/

          Download and install latest version of node.js. In my case, it is 11.8.0

          How to install Angular 7

          After the successful installation, you will see command prompt like this:

          How to install Angular 7

          Use npm to install Angular CLI

          Run the Angular CLI command to install Angular CLI

          npm install -g @angular/cli  
          How to install Angular 7

          or

          Just go to Angular CLI official website https://cli.angular.io/

          You will see the whole cli command to create an Angular app. You need to run the first command to install Angular CLI. These steps are same for Windows and Mac.

          How to install Angular 7
          npm install -g @angular/cli  
          
          ng new my-dream-app  
          
          cd my-dream-app  
          
          ng serve 

            Your Angular 7 Environment setup is complete now.

          1. Angular Features

            A list of most important features and benefits of Angular:

            Angular supports multiple platforms

            Angular is a cross platform language. It supports multiple platforms. You can build different types of apps by using Angular.

            1. Desktop applications: Angular facilitates you to create desktop installed apps on different types of operating systems i.e. Windows, Mac or Linux by using the same Angular methods which we use for creating web and native apps.
            2. Native applications: You can built native apps by using Angular with strategies from Cordova, Ionic, or NativeScript.
            3. Progressive web applications: Progressive web applications are the most common apps which are built with Angular. Angular provides modern web platform capabilities to deliver high performance, offline, and zero-step installation apps.

            High Speed, Ultimate Performance

            Angular is amazingly fast and provides a great performance due to the following reasons:

            1. Universal support: Angular can be used as a front-end web development tool for the programming languages like Node.js, .Net, PHP, Java Struts and Spring and other servers for near-instant rendering in just HTML and CSS. It also optimizes the website for better SEO.
            2. Code splitting: Angular apps are fast and loads quickly with the new Component Router, which delivers automatic code-splitting so users only load code required to render the view they request.
            3. Code generation: Angular makes your templates in highly optimized code for today?s JavaScript virtual machines which gives the benefits of hand-written code.

            Productivity

            Angular provides a better productivity due to its simple and powerful template syntax, command line tools and popular editors and IDEs.

            1. Powerful templates: Angular provides simple and powerful template syntax to create UI view quickly.
            2. IDEs: Angular provides intelligent code completion, instant errors, and other feedback in popular editors and IDEs.
            3. Angular CLI: Angular CLI provides command line tools start building fast, add components and tests, and then instantly deploy.

            Full Stack Development

            Angular is a complete framework of JavaScript. It provides Testing, animation and Accessibility. It provides full stack development along with Node.js, Express.js, and MongoDB.

            1. Testing: Angular provides Karma and Jasmine for unit testing. B y using it, you can check your broken things every time you save. Karma is a JavaScript test runner tool created by Angular team. Jasmine is the testing framework form unit testing in Angular apps, and Karma provides helpful tools that make it easier to us to call our Jasmine tests whilst we are writing code.
            2. Animation Support: Angular facilitates you to create high-performance, complex choreographies and animation timelines with very little code through Angular’s intuitive API.
            3. Accessibility: In Angular, you can create accessible applications with ARIA-enabled components, developer guides, and built-in a11y test infrastructure.
          2. What is Angular 7?

            Angular 7 is a JavaScript (actually a TypeScript based open-source full-stack web application) framework which makes you able to create reactive Single Page Applications (SPAs). Angular 7 is completely based on components. It consists of several components which forms a tree structure with parent and child components. Angular’s versions beyond 2+ are generally known as Angular only. The very first version Angular 1.0 is known as AngularJS.

            “Angular is a complete rewrite of AngularJS by the same team that built AngularJS.”

            What is Single Page Application (SPA)?

            A single page application is a web application or a website which provides users a very fluid, reactive and fast experience similar to a desktop application. It contains menu, buttons and blocks on a single page and when a user clicks on any of them; it dynamically rewrites the current page rather than loading entire new pages from a server. That’s the reason behind its reactive fast speed.

            Difference between AngularJS and Angular

            AngularJSAngular
            AngularJS is common and popular name of the first version of Angular1.0.Angular is common and popular name of the Angular’s version beyond 2+
            AngularJS is a JavaScript-based open-source front-end web framework.Angular is a TypeScript-based open-source full-stack web application framework.
            AngularJS uses the concept of scope or controller.Instead of scope and controller, Angular uses hierarchy of components as its primary architectural characteristic.
            AngularJS has a simple syntax and used on HTML pages along with the source location.Angular uses the different expression syntax. It uses “[ ]” for property binding, and “( )” for event binding.
            AngularJS is a simple JavaScript file which is used with HTML pages and doesn’t support the features of a server-side programming language.Angular uses of Microsoft’s TypeScript language, which provides Class-based Object Oriented Programming, Static Typing, Generics etc. which are the features of a server-side programming language.
            AngularJS doesn’t support dynamic loading of the page.Angular supports dynamic loading of the page.

            Angular 7 vs Angular6 vs Angular2 vs Angular1

            Angular1 was initially released in 2010. It was the first Angular version. It created a revolution in the web application development. It was a browser side JavaScript which was used within HTML code. It is popularly known as AngularJS.

            Angular2 was a complete rewrite of Angular1. It was initially released in 2016. There is nothing common between Angular2 and Angular1 except the core developer’s team. Angular2, Angular 6 and Angular 7 are very similar to each other. Angular 7 is the latest version. It contains the extensive features of Angular2 and Angular6. These versions are known as Angular.