Professional Documents
Culture Documents
- Component comprises of
o Presentation in HTML
o Styles in CSS
o Functionality in TypeScript
- Technically Angular component is a class configured with functionality.
- Component behaviour is given by using �@Component()� decorator
- �@Component()� is derived from �@angular/core� library.
Syntax:
import { Component } from �@angular/core�;
@Component()
export class HeaderComponent
{
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
o If you want your application to start with specific component then it must be
configured in
bootstrap:[ ]
Note: �bootstrapping� is the process of converting static DOM into dynamic DOM.
Before angular take control it is Static DOM
After the angular framework take control, it is Dynamic DOM
Adding a component into your application
- You can add manually by configuring the files into �app� folder
- You can add by using �Angular CLI� commands, which can scaffold [Ruby] the
content.
- You can import and inject 3rd party components into application
o Kendo Components [Telerik]
o Material Components
o Bootstrap Components
@Component({
selector: 'app-header',
template: `
<div>
<h2>{{title}}</h2>
<p> Online Shopping - 70% OFF on Electronics </p>
</div>
`,
styles: ['h2{background-color:red; color:white; text-align:center}','p
{color:blue}']
})
@NgModule({
declarations: [
AppComponent,
HeaderComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [HeaderComponent]
})
- Go to �Index.html�
<body>
<app-header></app-header>
</body>
- Login.component.ts
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent
{
title:string = 'User Login';
}
- Login.component.html
<div class="container">
<div class="box">
<h3>{{title}}</h3>
<dl>
<dt>User Name</dt>
<dd><input type="text"></dd>
<dt>Password</dt>
<dd><input type="password"></dd>
</dl>
<button>Login</button>
</div>
</div>
- Login.component.css
.container {
display: flex;
justify-content: center;
align-items: center;
height: 400px;
}
.box {
border:2px solid darkcyan;
border-radius: 10px;
padding: 20px;
}
Login.component.html
<div class="container-fluid justify-content-center align-items-center d-flex"
style="height: 400px;">
<div>
<h3>{{title}}</h3>
<dl>
<dt>User Name</dt>
<dd class="input-group">
<span class="bi bi-person input-group-text"></span>
<input type="text" class="form-control">
</dd>
<dt>Password</dt>
<dd class="input-group">
<span class="bi bi-key-fill input-group-text"></span>
<input type="password" class="form-control">
</dd>
</dl>
<div class="d-grid">
<button class="btn btn-primary">Login</button>
</div>
</div>
</div>
Databinding in Angular
- Databinding is a technique used in applications to access data from store and
present in UI. Identifying the changes in UI and update back to store.
- JavaScript and jQuery use lot of DOM manipulations for Data Binding.
- Angular is completely de-coupled from DOM manipulations, it uses client-side
frameworks like
o MVC
o MVVM
- Databinding is categorized into 2 types
o One-Way-Binding
o Two-Way-Binding
Ex:
Databinding.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-databinding',
templateUrl: './databinding.component.html',
styleUrls: ['./databinding.component.css']
})
export class DatabindingComponent {
Product:any = {
Name: 'Samsung TV',
Price: 46000.55,
Stock: true
}
isInvalid = false;
}
Databinding.component.html
<div class="container-fluid mt-3">
<h3>Product Details</h3>
<dl>
<dt>Name</dt>
<dd [innerHTML]="Product.Name"></dd>
<dd>
<input type="text" [value]="Product.Name" >
</dd>
<dt>Price</dt>
<dd>{{Product.Price}}</dd>
<dt>Stock</dt>
<dd>{{(Product.Stock==true)?"Available":"Out of Stock"}}</dd>
</dl>
<button [disabled]="isInvalid" class="btn btn-primary">Add Product</button>
</div>