Sunday, December 15, 2019

Pagination in Angular

Pagination in Angular


To use Pagination NgxPaginationModule need to install

npm install --save ngx-pagination

once this is installed then add this module in app.module.ts and in import array

app.module.ts 

import { NgxPaginationModule } from  'ngx-pagination';

imports: [
        NgxPaginationModule,
   
    ],


app.Component.Html


in HTML we have create a list item in which we will apply pagination .

we have used paginate event to apply this . in ngFor itemsparpage is used to see how many item will be displayed .

<h1>Employee List</h1>
<ul>

    <li *ngFor = "let item of EmpListcollection | paginate: { itemsPerPage: 10, currentPage: p } " >
    {{item}}

</li>
</ul>

<pagination-controls (pageChange)="p = $event"></pagination-controls>

app.component.ts

in type script file we have created a collection which will be binded in list in HTML component

import { ComponentOnInit } from '@angular/core';
import { stringify } from 'querystring';


@Component({
  selector: 'app-component',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class appComponent implements OnInit {

  EmpListcollection = [];
  mystring : string ;

  constructor() { }

  ngOnInit() {
    
    for (let i =1;i<50;i++)
          {
            this.mystring = 'Angular';
            this.mystring = this.mystring.concat('  Tyepscript ' + i + '.0');
            this.EmpListcollection.push(this.mystring)
          }
  }
}


Output .


Follow us on Shubh Techno Expert 








How to use Service in Angular App

How to call service in  Angular App      


component can not fetch data directly from database. so to fetch data from database API need to call in Angular App . With the help of service we can access method and properties across other component in the entire Projects

1. For this We need a Web API which we will call in our App to access the data .

Syntax to create Service

ng generate service servicename 

API URL : http://localhost:37187/Api/ViewContact/ViewEmpDetails

This API will Return data in Json Format .

    {
        "EMPCode": "E0001",
        "EmpName": "Nagendra1",
        "Email": "Nagendra@gmail.com",
        "Salary": "2500"
    },
   {
        "EMPCode": "E0002",
        "EmpName": "Nagendra2",
        "Email": "Nagendra@gmail.com",
        "Salary": "2700"
    },


Add this Module in App.Module.ts file 

import { HttpClient } from '@angular/common/http';

and add in import array also

import:[

HttpClientModule

       ]


see this full VDO on youtube on Shubh Techno Expert  Call web API in Angular

app.component.html

Here in HTML added one button in which called a method this method GetEmpDetails() will call the service and return the data .

And taken one table to bind this data .


<h1 align = "center">How to use Service in Angular App</h1>

<button type = "button" (click) ="GetEmpDetails()"  align ="Center" >Click Me</button>

<table border ="3">
    <th>EmpName</th><th>EmpCode</th><th>Salary</th><th>Email</th>

    <tr *ngFor = "let item of EmpServiceresponse">

        <td> {{item.EmpName}}</td>
        <td>{{item.EMPCode}}</td>
        <td> {{item.Salary}}</td>
        <td>{{item.Email}}</td>

    </tr>

</table>

app.component.ts

in app.component we have created the method to call the service . and initiated the service in constructor .

import { Component } from '@angular/core';
import {EmpserviceServicefrom './empservice.service'

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

  constructor(private _EmpserviceService:EmpserviceService ){}
GetEmpDetails(){

  this._EmpserviceService.GetEmployeDetails().subscribe((respp)=>
  {
    this.EmpServiceresponse = respp;
    console.log(this.EmpServiceresponse);

  })
}
}

empentities.ts

This is the Entities in which data will be mapped.

export class Empentities {

    EMPCodestring;
    EmpNamestring;
    Emailstring;
    Salary : number;
}

empservice.service.ts

we have created this service by below command 

ng g service Empservice

once service is created then we will provide API url and call it in a method and provide its return type as JSON

import { Injectable } from '@angular/core';
import { HttpClient , HttpResponseHttpHeaders } from '@angular/common/http';  
import { Empentities } from './model/empentities';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class EmpserviceService {
  
 url = 'http://localhost:37187/Api/ViewContact/';  

  constructor(private http:HttpClient) { }

  GetEmployeDetails(): Observable<Empentities[]> {  
    
    return this.http.post<Empentities[]>(this.url+'/ViewEmpDetails'
    {headers:new HttpHeaders({'Content-Type':'application/json'})});
  }  

}

app.module.ts

In App.module we will register our service and add the HttpClientModule Module in Import array .

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { EmpserviceService } from './empservice.service';


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


output

click on Button you will get the reponse 

Right click chosse inpect Element  and see the response on Console


Follow us :   Shubh Techno Expert on Youtube 

Tuesday, December 10, 2019

LocalStorage and session storage in Angular

How to access Local and Session variable in Angular


To Set Value in Session storage

Sessionstorage.SetItem('key','value')

To get Value from Session storage

Sessionstorage.getitem('key')

To set Value in Local storage

localstorage.SetItem('key','value')

To get Value from Local storage

sessionstorage.getitem('key')

How to Clear Session variable

sessionstorage.clear()

How to remove all session items .

sessionstorage.removeitem('key')

local storage works like Viewstate and session storage work like session variable .

http://xafdeveloper.blogspot.com

https://youtu.be/povG03z340E

Saturday, November 30, 2019

Angular Production Build


Build Angular App for Production


while you plan for production release then keep these things in Mind

ng build --prod --base--href /vdname/

above command will create a production build in your app folder . a dist folder will be added in your app folder with name "dist".

This dist folder will have all build files .

Note1 : keep in mind that vdname and your actual vd name where you will do the deployment always                   will be the same .
suppose your hosting address is http://www.xafdeveloper.blogspot.com
then while making build you need to create build like this .

ng build --prod --base--href   /xafdeveloper/

Note2: URL Rewrite extension need to install in IIS and you can download it from Microsoft site .

Note3: you need to add web.config file in your vd as by default angular build didn't create a                                   "web.config" file .

you can create is by adding a text file and change its name and file type as config .
now copy url rewrite code from web and paste it in this file .
Now access your app and enjoy Angular Application Cheers !..
Note3. you can see base reference details in Index.html file also in your build folder


@ShubhTechnoExpet on Facebook page .



Tuesday, November 26, 2019

Excel Download Feature in Angular


For Excel Download feature in Angular these Modules are required . you need to install.


1. file-saver

2. xlsx

npm install file-saver --save

npm install xlsx --save

once you install these package then it will give some error and ask for fund but dont wory your package will be installed which is required for excel download .

you need admin right to intall these packages.

 You Can Follow my Channel for VDO Shubh Techno Expert

Sunday, November 24, 2019

Dropdown binding in Angular 6 by *ngFor


Dropdown binding in Angular 6 by *ngFor


Looping can be achieved by *ngFor directive

Syntax     *ngFor="let item of collection"


 <select id="slemp" (change)="GetEmp($event)">

        <option *ngFor="let item of Empcollection" value={{item.EmpCode}} >
                            
                            {{item.EmpName}}
        </option>
    </select>

Here one Dropdown is created and its value and text is shown in an alert box .  we have created a GetEmp($event) method and passed event as an argument by which we will get the control value .


you can Follow Shubh Techno Expert

Salesperson.Component.Html

<div>
<h1 style="text-align: center;">Dropdown binding in Angular by *ngFor</h1>
<div align="center">
<select (change)="myEmp($event)" >
    <option *ngFor ="let item of Empcollection" value={{item.EmpCode}}>{{item.EmpName}}</option>
</select>

</div>


Salesperson.Component.ts


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

@Component({
  selector: 'app-salesperson',
  templateUrl: './salesperson.component.html',
  styleUrls: ['./salesperson.component.css']
})
export class SalespersonComponent implements OnInit {

  constructor() { }

  ngOnInit() {

      }

 
      Empcollection: any =
      [
        { EmpCode: "E001",EmpName: "Nagendra",  Email: "Nagendra@test.com", Salary: 8000 },
        { EmpCode: "E002",EmpName: "Raj",  Email: "raj@test.com", Salary: 70000 },
        { EmpCode: "E003",EmpName: "Shubh",  Email: "Nagendra@test.com", Salary: 80000 },
        { EmpCode: "E004",EmpName: "Techno",  Email: "Shubh@test.com", Salary: 9000 }
  
      ]

      myEmp(event)
      {
        alert("Emp Name Selected :" + event.target.value);
      }
     
}


Looping in Angular by *ngFor

Looping can be achieved by *ngFor directive

Syntax     *ngFor="let item of collection"

Here collection object may be your collection of any class or array
     item is the variable in which you will get each object which is available in your collection.


you can Refer on my Channel

                Shubh Techno Expert

https://youtu.be/3dYC1PTXmfI

Here is Example .

Salesperson.component.html 

<h1 style="text-align: center;">Looping through *ngFor</h1>

<div>
    <h1 align="center">Employee</h1>
    <table border="2" align="center">
        <thead>
            <th>Emp Name</th>
            <th>Emp Code</th>
            <th>Email</th>
            <th>Salary</th>
        </thead>
        <tbody>
            <tr *ngFor="let item of Empcollection">
                <td>{{item.EmpCode}}</td>
                <td>{{item.EmpName}}</td>
                <td>{{item.Email}}</td>
                <td>{{item.Salary}}</td>
            </tr>
        </tbody>

    </table>

Salesperson.component.ts 

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

@Component({
  selector: 'app-salesperson',
  templateUrl: './salesperson.component.html',
  styleUrls: ['./salesperson.component.css']
})
export class SalespersonComponent implements OnInit {

  constructor() { }

  ngOnInit() {

      }
      Empcollection: any =
      [
        { EmpCode: "E001",EmpName: "Nagendra",  Email: "Nagendra@test.com", Salary: 8000 },
        { EmpCode: "E002",EmpName: "Raj",  Email: "raj@test.com", Salary: 70000 },
        { EmpCode: "E003",EmpName: "Shubh",  Email: "Nagendra@test.com", Salary: 80000 },
        { EmpCode: "E004",EmpName: "Techno",  Email: "Shubh@test.com", Salary: 9000 }
  
      ]
     
}

Saturday, November 23, 2019

Usefull Command In Angular

How to Create new Application in Angular

ng new appname

How to Create New Component

ng generate component componentname

ng g c Component Name

if you want to create component in a folder then specify folder name also

ng g c emp/EmployeeDetails


g -- generate and c -- component

How to create New Class

ng generate class classname

How to Create New Service 

ng g service servicename

Tuesday, November 19, 2019

Angular Build Minification and Uglification

Minification and Uglyfication in Angular Deployement

Minification : While building Angular Application white spaces , unused variable , unused token removal is called Minification and it reduced build size .


uglyfication : While building Angular app variable name , function name class and other things name changes and compiled so after build in view source file you will not able to see the script , vairable class and other things . it also reduce files and insure security. its called Uglification .


you can make build by

ng build --dev  For Development

ng build --prod For Production Release

after building you cann see the build package in dist Folder in your currwnt app.

all bukld will will be in compressed and combined format.

Saturday, November 16, 2019

ngIf in Angular 6



The *ngIf directive is most commonly used to conditionally show an inline template

The ngIf directive can be very useful in scenarios where you want to show a part of your Angular application UI

see my VDO on Shubh Techno Expert

https://youtu.be/eirevolrNYI


Content to render when condition is true.


Content to render when condition is

true.

----------------------------------------------------------------------------
Content to render when condition is true.



Content to render when condition is true.



True Condition Executed

False Condition Executed




-----------------------------------------------------------------------------


Content to render when condition is true.
Content to render when condition is false.


------------------------------------------------------------------------------

{{value}}

Content to render when value is null.


Customer.component.html

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

@Component({
  selector: 'app-customer',
  templateUrl: './customer.component.html',
  styleUrls: ['./customer.component.css']
})
export class CustomerComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

  flag: boolean = false; 
 
  empcollection: any = [
    {name: "Raj", email:"Raj@gmail.com" ,Salary:2000},
    {name: "Sumit", email:"sumit@gmail.com",Salary:70000},
    {name: "Nagendra", email:"Nagendra@Gmail.com",Salary:8000},
    {name: "Manish", email:"Manish@Gmail.com",Salary:9000}
    ]

}

customer.components.ts

<h1 align="center">how to use ngIf<h2>


        <div>
            <h1>Employee</h1>
            <table border="2">
                <thead>
                    <th>Name</th>
                    <th>Email</th>
                    <th *ngIf="flag">Salary</th>
                    <th *ngIf="flag">Actions</th>
                </thead>
                <tbody>
                    <tr *ngFor="let e of empcollection">
                        <td>
                            {{e.name}}
                        </td>
                        <td>
                            {{e.email}}
                        </td>
                        <td *ngIf="flag">
                            {{e.Salary}}
                        </td>
                        <td *ngIf="flag">
                            <button>Delete</button>
                            <button>Update</button>
                        </td>
                    </tr>
                </tbody>
            </table>

        </div>

        <table border="3">
            <th>Name</th>
            <th>Age</th>
            <tr *ngIf="false">
                <td>Amit</td>
                <td>12</td>
            </tr>
            <tr>
                <td>Rohit</td>
                <td>34</td>
            </tr>
            <tr *ngIf="true">
                <td>Deepak</td>
               <td>23</td>

            </tr>
        </table>
 
        

<div *ngIf="true; else loggedOut">
        Welcome friend.
      </div>
      
      <ng-template #loggedOut>
        Please friend, login.
      </ng-template>






Event Binding in Angular 6


we have taken a component to Show Event Binding .

See My VDO on Shubh Techno Expert

https://youtu.be/2LDYq7uKhzE

sidebar.component.html




   

Interpollation



   


       

           
                Email ID
               
               
           
           
                password
               
               

           
           
                 
               
               
             
           
           
                My Mail ID is :
               
               
           
       



   




sidebar.component.ts


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


@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent {

  myEmail:any;
  constructor() { }

  ngOnInit() {  }

  onClickSubmit(data) {
   debugger
    this.myEmail = data.emailid;
    alert("Entered Email id : " + data.emailid +"   " + data.passwd);
  }

  btnClicked()
  {
    console.log('i am clicked from HTML Page');
  }

  btnClickedwithParamter(evnt)
  {
    console.log('Button is clicked which Will return Click Event value') ;
    console.log(evnt) ;
  }
 
}


















Interpolation in Angular 6

Interpolation is a technique that allow the user to bind a value to a UI element . two curly braces are used for this {{}} . It is called a Template Expression

See my VDO on Shubh Techno Expert Channel 

app.component.html




   

Interpollation



   


       

           
                Email ID
               
                                ngModel width="100%">
           
           
                password
               
                                 ngModel width="100%">

           
           
                 
               
               
             
           
           
                My Mail ID is :
               
                             value="{{userlogin1.value.emailid}}">
           
       



   
    {{userlogin1.value.emailid}}
    This is incremented Value : {{1+ 1}}

    Sum of :


sum of : {{10 + 20 + 30}}


{{userlogin1.value.emailid}}


{{'This is My Email id ' + userlogin1.value.emailid}}




app.component.ts

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


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class appComponent {

  myEmail:any;
  constructor() { }

  ngOnInit() {  }

  onClickSubmit(data) {
   debugger
    this.myEmail = data.emailid;
    alert("Entered Email id : " + data.emailid +"   " + data.passwd);
  }

  
}

Friday, November 15, 2019

Angular 6 Routing


Routing in Angular


https://www.youtube.com/watch?v=-Rtf6G7u4-4&t=1s


Today i will tell you how to Create Link in Angular .

to Create a link you need a app-routing.module.ts
in this module we define link /routes .


Frist we will Create Some Link on our Home Page Component

app.component.html





Home


Employee







The tells the router where to display routed views.

Now our HTML page in created in which two link will be there
on clicking these link you will be redirected on another page .
this is done through routing .

Routing outlet is used for routing .


we have create component

C:\MyAngular\myapp> ng g c home
CREATE src/app/home/home.component.html (19 bytes)
CREATE src/app/home/home.component.spec.ts (614 bytes)
CREATE src/app/home/home.component.ts (261 bytes)
CREATE src/app/home/home.component.css (0 bytes)
UPDATE src/app/app.module.ts (553 bytes)

C:\MyAngular\myapp> ng g c emp
CREATE src/app/emp/emp.component.html (18 bytes)
CREATE src/app/emp/emp.component.spec.ts (607 bytes)
CREATE src/app/emp/emp.component.ts (257 bytes)
CREATE src/app/emp/emp.component.css (0 bytes)
UPDATE src/app/app.module.ts (623 bytes)

see in both case app.module.ts is updated .

in app.module.ts routing module is already added in import section

by default import { AppRoutingModule } from './app-routing.module'; is added in app.module.ts


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

const routes: Routes = [
{ path:'',component : HomeComponent } ,
{ path:'employee',component : EmplistComponent }

];
path: a string that matches the URL in the browser address bar.
component: the component that the router should create when navigating to this route.

---------------------------------

Angular 6 Project Structure

Learn the Project Structure of Angular 6

Shubh Techno Expert

https://www.youtube.com/watch?v=povG03z340E

you will able to understand the Project Structure .


Project Architecture
app-routing.module.ts
This Module is used to define Routing
app.component.html
This HTML file is used for UI and HTML designing
app.component.spec.ts
This file is used for Unit Testing .
app.component.ts
This is typescript File where you can write your script .
app.module.ts
This is the Module where you can import and define modules.
Index.html
This is the Startup file
angular.json
Here you can set your Botstrap css file and script files
Src Folder
   This Folder Contains all your Files
.editorconfig
This is the Configration File of Project

Angular 6 Tutorials

Angular is a JavaScript open-source front-end web application framework. 

Learn and Start Angular 6 


https://youtu.be/iCczsazLY-E

Prerequisites

1. Node.js

2. npm package manager

3.Install the Angular CLI

open command Prompt and Install 

c: \ > npm install -g @angular/cli



Mat Table Angular

Mat Table in Angular Mat table is used to display data . its a material designed styled data-table . For using Material Table MatTableMo...