Sunday, January 8, 2023

Tab Menu Nav in angular 13

 We have three tab menu in main-tabmenu.component page 
Tab-1 / Tab-2 / Tab-3

Tab-1 default will display, onclick "Move to tab-2" button in the tab-1 then will move to tab-2.

Previous and next button in tab2.component.html page.

 


main-tabmenu.component.html
---------------------------------------


<p>TAB Menu Main Page</p>


<h1 style="background-color: #f00; color: #fff;">{{msg}}</h1>


<div *ngIf="tab1Div">

    <app-tab1 (Tab2DivShow)="EnableTab2($event)" ></app-tab1>

</div>


<div *ngIf="tab2Div" >

    <app-tab2 (Tab3DivShow)="EnableTab3($event)" (Tab1DivShow)="EnableTab1($event)"></app-tab2>

</div>


<div *ngIf="tab3Div">

    <app-tab3></app-tab3>

</div>

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


main-tabmenu.component.ts
----------------------------


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


@Component({
  selector: 'app-components',

  templateUrl: './components.component.html',

  styleUrls: ['./components.component.css']
})

export class ComponentsComponent {

  tab1=true;
  tab2=false;
  tab3=false;

  tab1Div = true;
  tab2Div = false;
  tab3Div = false;


  EnableTab1(EnableTab1:any){
    this.disableTabs();
    this.tab1Div = true;
  }


  EnableTab2(EnableTab2:any){
    this.disableTabs();

    this.tab2Div = true; 
}


  EnableTab3(EnableTab3:any){
    this.disableTabs();
    this.tab3Div = true;
  }

  disableTabs(){
    this.tab1Div = false;
    this.tab2Div = false;
    this.tab3Div = false;
  }
}

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


tab1.component.html
----------------------------

<button (click)="tab2DivDisplay()">Tab-2</button>



tab1.component.ts
----------------------------

import { Component, EventEmitter, Input, Output } from '@angular/core';


@Output() public Tab2DivShow = new EventEmitter();


tab2DivDisplay(){
  this.Tab2DivShow.emit("This is from Child Message");
}



tab2.component.html
----------------------------

<button (click)="tab1DivDisplay()">Previous-Tab1</button>

<button (click)="tab3DivDisplay()">tttTab-3</button>



tab2.component.ts
----------------------------

import { Component, EventEmitter, Input, Output } from '@angular/core';


  @Output() public Tab1DivShow = new EventEmitter();

  @Output() public Tab3DivShow = new EventEmitter();

  

  tab3DivDisplay(){
    this.Tab3DivShow.emit("This is from Child Message");
  }


  tab1DivDisplay(){
    this.Tab1DivShow.emit("This is from Child Message");
  }


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

Folder Structure
---------------------

app / comp


@Input - @Output

 

@Input : - Parent to child communication.

@Output :-  Child to parent 


Parant Component

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

parent-component.html

<app-parent-component (childInfo)="msg=$event" [fromParent]="parentMsg"></app-parent-component>


<h1 style="background-color: #f00; color: #fff;">{{msg}}</h1>


parent-component.ts


 public parentMsg = "This is parent Message";

  msg: any;


Child Component

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


child-component.html

<h1 style="background-color: blue; color: #ffffff; padding: 10px 20px; text-align: center;">

    {{parent}}

</h1>


<button (click)="fireEvent()">Tab-1</button>


child-component.ts

import { Component, EventEmitter, Input, Output } from '@angular/core';


@Input('fromParent') public parent: any;

@Output() public childInfo = new EventEmitter();


fireEvent(){

    this.childInfo.emit("This is from Child");

}







Tuesday, November 9, 2021

Property Binding Angular-10

 Property Vs Attributes


Attributes and properties are not the  same

Attributes are defined by HTML

Properties are defined by DOM 

Attributes initialize DOM properties and then they are done

Attributes values con't change one they are initialized

Property values however can change




<input type="text" value="Angular Bin" >

<input type="text" [id]="userID" value="Angular Bin" >

<input type="text" disabled="false" id={{userID}} value="Angular Bin" > (Not work disabled)

<input type="text" [disabled]="false" id={{userID}} value="Angular Bin" > (It will work disabled)

<input type="text" [disabled]="isDisabled" id={{userID}} value="Angular Bin" > (It will work disabled)

<input type="text" bind-disabled="isDisabled" id={{userID}} value="Angular Bin" > (It will work disabled)






component.ts


public userID = "Angular 8"

public isDisabled = true;



Interpolation in Angular-10

 

Interpolation 

By using {{ }} we can use the property value only

String interpolation is One Way Data Binding

Which is used to output the data from typescript code

Simply, interpolation  is way of binding data from class to template




<p>Welcome to {{title}}</p>

<p>{{"Welcome to :" + title}}</p>

<p>{{title.length}}</p>

<p>{{title.toUpperCase()}}</p>

<p>{{userName()}}</p>

<p>{{myUrl}}</p>




component.ts file


public title = "Angular Bin";

public myUrl = window.location.href;


userName(){

   return "Welcome : " + this.title;

}



Saturday, August 15, 2020

How to display image in angular 9

 

Browse with default image in a form
After select image display View in a form

image.component.html

<div class="col-md-2 form-group"> <input type="file" class="form-control" #Image accept="image/*" (change)="handleFileInput($event.target.files)" required> </div> <div class="col-md-1 form-group"> <img [src]="imageUrl" style="width: 50px; height:50px;border:1px solid #f60;" /> </div>

image.component.ts

export class ImageComponent implements OnInit { imageUrl: string = "/assets/img/default-img.png"; fileToUpload: File = null; constructor() { }
handleFileInput(file: FileList) { this.fileToUpload = file.item(0); // Show image Preview var render = new FileReader(); render.onload = (event: any) => { this.imageUrl = event.target.result; } render.readAsDataURL(this.fileToUpload); }

assets --> img (folder Name)--> default-img.png 


Browse with default image in a form
After select image display View in a form


Tuesday, August 11, 2020

SQL Database with API in Angular CRUD Services

 

Create WebAPI project in Visual Studio to Project folder

Select File Name  à  OK à Web (Select in Left Side) à Select  ASP .Net Web Application (.Net Framework)  à OK

Select Web API à OK

 

Create Database Table

                Table  name is EmployeeDB

                Create new table in EmployeeDB

                                Create columns in a table

 

Save after choose name file (Employee) à OK

Come to Visual Studio right click on Models à Add New Item à


select left side Data à Select right side ADO.Net Entity Data Model -- > Enter DBModels à Add

Next


Click on New Connection…

 



Click on ADD button


Created controller

Completed Web API and Connected to DataBase

 

 

Create Angular NEW PROJECT

In terminal

Create ng g c employee-data

Create ng g c employee-data/employee

Create ng g c employee-data/employee-lists

Create folder ShareEmployee in employee-data

ShareEmployee open in Terminal  à ng g class Employee

                                                                   à ng g s employee

npm install --save  @angular/http

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
i
mport { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { EmployeeDataComponent } from './employee-data/employee-data.component';
import { EmployeeComponent } from './employee-data/employee/employee.component';
import { EmployeeListsComponent } from './employee-data/employee-lists/employee-lists.component';
import { from } from 'rxjs';

@
NgModule({
 
declarations: [
    AppComponent,
    EmployeeDataComponent,
    EmployeeComponent,
    EmployeeListsComponent,

    ],
 imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    HttpModule

  ],
 
providers: [],
 
bootstrap: [AppComponent]
})

export class AppModule { }

 

employee-data.component.html

<div class="bg-dark text-white text-center" style="padding:20px; margin-bottom: 20px;">
<
h3>Employee Details
<a [routerLink]="['/Students']" class="btn btn-primary pull-right">Students</a>
</
h3>
</
div>

<div class="row">
<
div class="col-md-4" style="padding:0px">
    <
app-employee></app-employee>
</
div>
<
div class="col-md-8">
    <
app-employee-lists></app-employee-lists>
</
div>
</
div>


employee-data à employee à employee.component.html

<form class="emp-forms" #employeeForms="ngForm" (ngSubmit)="onSubmit(employeeForms)">
<
input type="hidden" class="form-control" placeholder="Employee ID" name="EmployeeID" #EmployeeID="ngModel"
                     [(ngModel)]="employeeservice.SelectedEmployee.EmployeeID">
<
div class="col-md-12 form-group">
   <
input type="text" class="form-control" placeholder="Employee Name" name="EmployeeName" #EmployeeName="ngModel"                      [(ngModel)]="employeeservice.SelectedEmployee.EmployeeName">
</div>

<div class="col-md-12 form-group">
<
input type="text" class="form-control" placeholder="Employee Code" name="EmployeeCode" #EmployeeCode="ngModel"
       [(ngModel)]="employeeservice.SelectedEmployee.EmployeeCode">
</
div>
<
div class="col-md-12 form-group">
<
input type="text" class="form-control" placeholder="Employee TP" name="EmployeeTP" #EmployeeTP="ngModel"
       [(ngModel)]="employeeservice.SelectedEmployee.EmployeeTP">
</
div>
<div class="col-md-12 form-group">
<input type="text" class="form-control" placeholder="Email ID" name="EmployeeEmail" #EmployeeEmail="ngModel"
       [(ngModel)]="employeeservice.SelectedEmployee.EmployeeEmail">
</div>
<div class="col-md-12 form-group">
<button type="submit" class="btn btn-primary">Submit</button> &nbsp;
<button type="button" class="btn btn-secondary" (click)="resetButton(employeeForms)">Reset</button>
</div>
</form>


employee-data à employee à employee.component.ts

import { Component, OnInit } from '@angular/core';
import { Employee } from '../ShareEmployee/employee';
import { EmployeeService } from '../ShareEmployee/employee.service';
import { NgForm } from '@angular/forms';

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

export class EmployeeComponent implements OnInit {
 
constructor(public employeeservice: EmployeeService) {  }
 
 
ngOnInit() {
  
this.employeeservice.SelectedEmployee = {
     
EmployeeID: null,
     
EmployeeCode: '',
     
EmployeeEmail: '',
     
EmployeeName: '',
     
EmployeeTP: ''
     }
 }

     
resetButton(form: NgForm) {
     
if (form != null) form.reset();
      }

 

  onSubmit(form: NgForm) {
    if (form.value.EmployeeID == null) {
     
form.value.EmployeeID = 0;
     
this.employeeservice.PostEmployees(form.value).subscribe(data => {
        
this.resetButton(form);
    })
     }
else{
     
this.employeeservice.PutEmployees(form.value.EmployeeID, form.value).subscribe(data =>{
     
this.resetButton(form);
       
this.employeeservice.GetEmployee();
      })
    }

  }
}


employee-data à employee à employee-lists.component.html

<table class="table table-hover">
<thead>
<tr>
<
th>Sno</th>
<
th>ID</th>
<
th>Name</th>
<
th>TP</th>
<
th>Email</th>
<
th>Actions</th>
</
tr>
</
thead>
<
tbody>
<
tr *ngFor="let employee of employeeservice.EmployeeLists">
<
td>{{employee.EmployeeID}}</td>
<
td>{{employee.EmployeeCode}}</td>
<
td>{{employee.EmployeeName}}</td>
<
td>{{employee.EmployeeTP}}</td>
<
td>{{employee.EmployeeEmail}}</td>
<
td>
<
a routerLink="#" class="text-primary" (click)="editEmployee(employee)"><i class="fa fa-edit"></i></a> &nbsp;
 <a class="text-danger" (click)="onDelete(employee.EmployeeID)"><i class="fa fa-trash"></i></a>
</
td>
</
tr>
</
tbody>
</
table>

  

employee-data à employee à employee-lists.component.ts

import { Component, OnInit } from '@angular/core';
import { EmployeeService } from '../ShareEmployee/employee.service';
import { Employee } from '../ShareEmployee/employee';

@
Component({
 
selector: 'app-employee-lists',
 
templateUrl: './employee-lists.component.html',
 
styleUrls: ['./employee-lists.component.css']
})

export class EmployeeListsComponent implements OnInit {
 
constructor(public employeeservice: EmployeeService) { }
 
ngOnInit() {
    
this.employeeservice.GetEmployee();
  }

editEmployee(emp: Employee) {
    
this.employeeservice.SelectedEmployee = Object.assign({}, emp)
  }

onDelete(id: number) {
    
if (confirm('Do you want to Delete this Employee Details ?') == true) {
      
this.employeeservice.DeleteEmployee(id).subscribe(x => {
      
this.employeeservice.GetEmployee();
    })
   }
  }
}

employee-data à share-employee à employee.ts

export class Employee {
    
EmployeeID: number;
    
EmployeeName: string;
    
EmployeeCode: string;
    
EmployeeTP: string;
    
EmployeeEmail: string;
}

employee-data à share-employee à employee.service.ts

import { Injectable } from '@angular/core';
import { Employee } from '../ShareEmployee/employee';

import { Http, Response, Headers, RequestOptions, RequestMethod } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';

@
Injectable({
  providedIn: 'root'
})

export class EmployeeService {

SelectedEmployee: Employee;
EmployeeLists: Employee[];

constructor(private http: Http) { }

PostEmployees(emplo: any) {
  return this.http.post('http://localhost:61917/api/Employees', emplo).map(x => x.json());
}

PutEmployees(id, emplo) {
  
return this.http.put('http://localhost:61917/api/Employees/' + id, emplo).map(x => x.json());
}

GetEmployee(){
this.http.get('http://localhost:61917/api/Employees').map((data:Response) => {
return data.json() as Employee[];
}).
toPromise().then(x => {
this.EmployeeLists = x;
})
}

DeleteEmployee(id : number){
return this.http.delete('http://localhost:61917/api/Employees/' + id).map(res => res.json());
}

}