Sunday, 25 October 2020

Angular - Nested Components Angular

Figure 1


 Adding a component to another component is Nested Components in Angular. 

From Figure 1, 

  • EmployeeDetails header should come from AppComponent 
  • And the below table contains employee Details should come from another component.

3 steps need to be followed to achieve this.

Step 1:

  • Create a new folder employeecomponent and add below files inside 
    • EmployeeComponent.ts
    • app.EmployeeComponent.html 
                    
Folder structure after adding files

Step 2:

Add below changes in the newly added files
    • EmployeeComponent.ts
    • import { Component } from "@angular/core";
      @Component(
          {
              selector:'Employees',
              templateUrl:'app.EmployeeComponent.html'  
          }
      )
      export class EmployeeComponent
      {
          FirstName : string = "Angular";
          LastName : string = "Tutorial";
      }
    • app.EmployeeComponent.html 
    • <table border="1px">
          <tr>
              <td>FirstName</td>
              <td>{{FirstName}}</td>
          </tr>
          <tr>
              <td>LastName</td>
              <td>{{LastName}}</td>
          </tr>
      </table>

Step 3:

Import the component in app.module.ts and add EmployeeComponent in declaration as below and compile and Run the application. We will get the desired output as seen at the top

import { EmployeeComponent } from "./employeecomponent/EmployeeComponent";

@NgModule({
  declarations: [
    AppComponent,     
    EmployeeComponent
  ],
........
........
})

Angular - template Vs templateUrl

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

@Component({selector:'my-App',
            template:`<h1>{{name}}</h1>`,
            templateUrl:'app.component.html'
})
export class AppComponent
{
  name : string = "name";} 


template and templateUrl can be used inside the decorator.

template:

  • When a template view content is very less, template property can be used.
  • This content can be present inside single/double quotes or backtick characters as displayed in the sample code.
  • Single/double quotes can be used whenever the content is in a single line
  • If the template view content has more than a line then, it is mandatory to use backtick character

DisAdvantages

  • Intellisense and formatting can't be done using Visual Studio / VS Code
  • Hard to read when Typescript and HTML are present together if HTML content is more

Example:

  • template:`<h1>{{name}}</h1>`,

templateUrl:

  • The external HTML file can be referred to when the content is more as referred in an initial screenshot
  • It provides better readability, Intellisense and formatting the content

Example:

  • templateUrl:'app.component.html'


Saturday, 24 October 2020

Angular - Typescript

 Typescript:

  • It is a programming language used to develop Angular applications
  • But it will be converted to Javascript before the browser executes
  • It is developed by Microsoft

Advantages of Typescript:

  • It is an open-source language
  • Superset of Javascript - i.e. It has the features of javascript with additional features
  • Strongly typed - everything has type
  • Class / Object-oriented programming language

To Practice Typescript


Angular - Anatomy Of Angular Application (Component)


The angular application comprises of Multiple components and Services. Services provide common functionalities to your components.

Angular Application Skeleton



Components:
The component is a class with a template and decorator
Components are made up of template, class and MetaData

  • Template - Defines UI. Contains HTML, Bindings and directives 
  • Class - Same as C# or any OOPS class which can be written using Typescript
    • Contains Properties and Functions
    • Properties provide data to Template and function provides logic to Template
  • MetaData - Class will be decorated with MetaData (Like attributes in C#)
    • Metadata will convert normal Typescript class to Angular component

Component Code:

  • By default root component in Angular named as AppComponent
  • Multiple components combined together and create a Module. Every Angular must have at least one module



Decorator:

  • The decorator will provide metadata to our class.
  • It can contain the properties which tell us how index.html page will use properties of a class. Consider the above example. The decorator has 3 properties. 
    • Selector - The value to this property will act as an HTML directive in index.html
    • templateUrl - Path of the HTML file whose content to be replaced within the selector directive at runtime
      • the template property also can be used if the content to be replaced within the directive is very small. In this case, HTML content will be hardcoded in the template property within backticks (` `)
  • The decorator can contain other properties too which is not mentioned in this example. Full list can be seen in Angular website


Angular - Setting Up initial code

 Below commands needs to run in Command Prompt to setting up the initial code

  • To set up a code, we need to install Angluar CLI. Follow the below steps.
    • Open command prompt and type below command
      • [sudo] npm install -g @angular/cli@latest  
        • sudo - Use only in Mac
        • -g - Global
  • Navigate to the path where you want to create your initial code for the project and Open Command Prompt
    • Type ng new [Your-Project-Name]
  • This will create initial code to work on as shown in below fig 1

  • Fig 1



Angular - List of software to be Installed

 

Software Installed to Learn Angular:    

Below are the only software required to getting started with Angular 

  • Visual Studio Code from here!
  • Node JS from here!
    • To confirm node is installed, Open Command Prompt and type Node and click enter
    • Below text will be seen in console depend on the version you installed
      Fig 1

Few other tools required 

  • Angular Framework and Libraries
  • Angular CLI 
  • Typescript
  • Testing Tools
But no need to install all these manually. This can be mentioned in Package.Json (Check later topics) and installed automatically

How Angular Applications are Boot strapped

 How Angular application is Bootstrapped? Angular applications are single-page applications. Only one HTML file will be available. only the ...