Sunday, June 17, 2018

Servicrs-2 (Countries Data)


create services folder

       ng g s services/countries

contries.service.ts
----------------------
//import Injectable
//Injectable class used to create the Custom Service
import { Injectable } from '@angular/core';
//import Http Class
//Http Class used to connect to server by using http protocol
//import Response
//"Response From Server" is the Response Type
import { Http,Response } from "@angular/http";
//import map(),catch()
//map() used to catch the positive Result
//catch() used to catch the Error Response
import "rxjs/Rx";
//import Observable
import { Observable } from "rxjs/Observable";
@Injectable()
export class CountriesService {
constructor(private _http:Http) { }
public getCountries():any{
return this._http.get("https://restcountries.eu/rest/v2/all")
.map((res:Response)=>{return res.json()})
.catch(this._handleError);
}
public _handleError(err){
console.error("Error Raised..."+err);
return Observable.throw(err || "Internal Server Error !");
}
}


if you get error in rxjs/Rx please install

 npm install --save rxjs-compat@6          if you get error again another

npm i rxjs-compat


           ng g c components/countries


service.component.ts
--------------------------

import { Component, OnInit } from '@angular/core';
import { CountriesService } from '../../services/countries.service';
import { HttpErrorResponse } from "@angular/common/http";

@Component({
selector: 'app-countries',
templateUrl: './countries.component.html',
styleUrls: ['./countries.component.css']
})
export class CountriesComponent implements OnInit {
private data: any;
constructor(private _service: CountriesService) { } //Dependency Injection

ngOnInit() {
this._service.getCountries().subscribe(res => this.data = res,
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
console.log("Client side Error");
} else {
console.log("Server Side Error");
}
});
}

}


countries.components.html
----------------------------------

<table border="1"
cellpadding="5px"
cellspacing="5px"
align="center"
style="font-size: 20px">

<thead style="background-color: gray">
<tr>
<th>Name</th>
<th>Capital</th>
<th>Flag</th>
<th>Currencies</th>
</tr>
</thead>

<tbody>
<tr *ngFor="let x of data">
<td>{{x.name}}</td>
<td>{{x.capital}}</td>
<td><img width="100px" height="50px" src="{{x.flag}}"></td>
<td>{{x.currencies[0].code}}</td>
</tr>
</tbody>
</table>

app.module.ts
-----------------

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { CountriesComponent } from './components/countries/countries.component';
import { CountriesService } from "./services/countries.service";
import { HttpModule } from "@angular/http";
@NgModule({
declarations: [
AppComponent,
CountriesComponent
],
imports: [
BrowserModule,
HttpModule
],
providers: [CountriesService],
bootstrap: [CountriesComponent]
})
export class AppModule { }


index.html
--------------
<body>
<app-countries></app-countries>
</body>


countries.components.spec.ts
------------------------------------
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { CountriesComponent } from './countries.component';

describe('CountriesComponent', () => {
let component: CountriesComponent;
let fixture: ComponentFixture<CountriesComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CountriesComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(CountriesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});



Wednesday, June 13, 2018

Predefined Services

**** Predefined Services ****

- The Services given by angular framework called as Predefined Services.


Predefined Services in Angular2:
---------------------------------------

1) Http
- Http is the predefined class in angular2.

- Http Class used to connect to servers via "http protocol".

- Http Class avaiable in "@angular/http" package.

- Http Class belongs to "HttpModule".

- HttpModule also available in "@angular/http" package.

- when ever we hit the server we are getting the "Observable" Response.

- map() is the predefined function used to catch the positive Observable Response.

- catch() is the predefined function used to catch the Negative Observable Response.

- "Positive Observable Response" is the "Response" Type.

- map(),catch() and next() functions are available in "rxjs/Rx" package.

- Observable also available in "rxjs" package.


Ex_1:


Step 1:
create the Angular6 Application

> ng new serEx1

- automatically "serEx1" project will be created.



Step 2:
create the custom service by using following command.


> ng g s services/countries

g stands for generate.

s stands for service


******************************************
serEx1
     src
app
          services
countries.service.ts
countries.service.spec.ts
******************************************

countries.service.ts
--------------------


Monday, June 11, 2018

Services - 01 - Custome Services

my.module.ts
------------------

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { MyComponent } from "./components/my.component";
import { MyService} from "./services/my.service"

@NgModule({
declarations: [
AppComponent,
MyComponent
],
imports: [
BrowserModule
],
providers: [MyService],
bootstrap: [MyComponent]
})
export class AppModule { }


create the folder in app in src - folder name is services

create file name - my.service.ts

my.service.ts
----------------

//import Injectable
//Injectable class used to create the custom service.
import { Injectable } from "@angular/core";

//Decorator
@Injectable()
export class MyService{
private data:any;

constructor(){
this.data = {
p_id:111,
p_name:"Product One",
p_cost:10000
};
};

public getData():any{
return this.data;
}

}

create the folder in app in src - folder name is components

create file name - my.component.ts
create file name - my.component.html


my.component.ts
---------------------


import { Component } from "@angular/core";
import { MyService} from "../services/my.service";

@Component({
selector:"cust-ser",
templateUrl:"./my.component.html"
})

export class MyComponent{
private data:any;
constructor(private _service:MyService){

}

ngOnInit(){
this.data=this._service.getData();
}
}


my.components.html
--------------------------

<h1>{{data | json}}</h1>


app.module.ts
-----------------

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { MyComponent } from "./components/my.component";
import { MyService} from "./services/my.service"

@NgModule({
declarations: [
AppComponent,
MyComponent
],
imports: [
BrowserModule
],
providers: [MyService],
bootstrap: [MyComponent]
})
export class AppModule { }



----

Sunday, June 10, 2018

Anonymous Functions

**** Anonymous Functions ****

- The Function without name called as Anonymous Function.

- Anonymous Functions also called as Arrow Functions in TypeScript.

- Anonymous Functions are more secured compared to named functions.

- Anonymous Functions utilizes the heap memory efficiently.


Syntax.
//function definition
var var_name = (argumants with data type):return_type =>{
//Biusiness Logic
}


//call the anonymous function
var_name();


Ex_1:
create the anonymous function with following variable
@fun_one

anonymous function return "Welcome...!" message

call the anonymous function.


Ex_2:
create the anonymous function with 3 arguments.

name of variable - @fun_one

@arg1 - string
@arg2 - string
@arg3 - string


call the anonymous function with 3 arguments.


Ex_3:
one arrow function return another arraow function.

inner arraow function return "Welcome...!" message


Ex_4:
create arrow function with 3 arguments.

while calling the arrow function ,
"pass the definitions of anonymous functions"
as arguments.


Ex_5:
     push 5 anonymous functions to empty array.


Ex_6:
create the nested arrow functions with number manipulations






Functions in Typescript

Functions

- Particular Business Logic called as Function.

- Functions are used to reuse the business logic.

- We can Declare the Functions with the help of "function" keyword.

- We Have Two Types of Functions.

1) Named functions

2) Anonymous Functions

Named functions
---------------
- The Function with the name called as Named function.


Syntax.
-------

function definition
-------------------
function function_name([arguments with datatype]):return type{

//Business Logic

}

//function calling
function_name();


Ex_1:
create the following function
@fun_one

"fun_one" return "Welcome...!"

    call the fun_one.


Ex_2:
=> create the folowing function with 3 arguments
@fun_one

=> argumnets are string type.

=> call the fun_one.


Ex_3:
=> create the two functions
@fun_one
@fun_two

=> "fun_one" return "fun_two" definition.

=> "fun_two" retrurn "welcome" message.

=> call "fun_one"


Ex_4:
create the following functions.

@fun_one
@fun_two


pass "fun_two" definition to "fun_one" as argument.


"fun_one" displays the argument.

"fun_two" return "welcome...!".


Ex_5:
create the following function with two arguments.
@fun_one

@arg1 - string type.
@arg2 - Array of Hetrogeneous Elements.

pass "fun_two" & "fun_three" to fun_one as array


Assignment:
-----------

create the following functions.
@fun_one
@fun_two
@fun_three
@fun_four
@fun_five

"fun_two" return "fun_three" definition.

"fun_three" return "Hello...!" message.

"fun_four" return "fun_five" definition.

"fun_five" return "Hi....!" message.

pass "fun_two" & "fun_four" definitions to "fun_one" as arguments.

"fun_one" calling should display "Hello...!" & "Hi...!" message


Variables in Typescript


  • TypeScript is the programming language.
  • TypeScript Introduced by microsoft.
  • Superset of JavaScript called as TypeScript.
  • TypeScript Follows OOPS.
  • Converting the TypeScript to Equalent JavaScript Called as "Transpilation".
  • Web Pages(HTML Files) won't understands th TypeScript.
  • As a TypeScript Developer we must perform the "Transpilation" Explicitely.
  • "tsc" is a compiler used to perform the transpilation.
  • "tsc" stands for "typescript compiler".



installation of TypeScript:
---------------------------
1) download and install NodeJS

website : https://nodejs.org/en/download/

file    : node-v8.11.2-x64.msi


2) download and install VisualStudioCode.

website : https://code.viGit-2.17.0-64-bitsualstudio.com/docs/?dv=win

file    : VSCodeSetup-x64-1.23.1.exe


3) download and install Git.

website : https://git-scm.com/download/win

file    : Git-2.17.0-64-bit.exe


4) install "tsc" by using following command.

> npm install -g tsc

- "npm" stands node packaging manager.

- "npm" is the integrated tool of NodeJS.

- "-g" stands for global installation.


variables in TypeScript:
------------------------
- Variables are used to store the data.
- By using Variables we can store all categories of Data.

Ex.
string
number
boolean
Array
Objects
  --
                          --
                          --

- we can declare variables by using either "var" keyword 
  or "let" keyword.

- we can save all the TypeScript files with the ".ts" extension.

- Transipilation gives the equalent javascript file.

Syntax.
------

var variable_name:data_type = "value"

Ex.
var data:string = "Welcome...!"


Transpilation of variables.ts file
----------------------------------

=> open the Integrated Terminal in Visual Studio Code

=> execute the following command in integrated terminal

> tsc variables.ts


create the webpage
------------------
index.html

<!DOCTYPE html>
<html>
<script src="variables.js"></script>
</html>