Angular JS-8
Angular JS-8
Angular
Creating Front End
(Always latest version)
About us
MKJ IT Learning is the corporate training firm, delivered enterprise level training across the
globe.
https://www.mkj-it-learnings.com/online-training-team-profiles
Setting up Development Environment
Check node Js
Discuss
• What is Objects.
• What is Behaviour
• dot operator
• [ ] operator
Hello World of Type Script
Variable Declarations
1) Typescript uses let & const keywork to declare variables.
2) One side JavaScript only has global scope and function scope , let & const solve this issue .
let don’t allow to re declare the variable and also provides block level support.
1) Creation of class
2) Adding a method
3) Extending the relationship
Self Learning
1) Modules
2) Decorators (more we discuss during angular.js)
Introduction of Angular What & Why?
1) What is Angular
2) Modular Approach
3) Re-Usable Code.
5) Unit testable
1) node
2) Npm (node package manager)
3) Angluar cli
Angular CLI Commands
Purpose Command Shortcut Command
New Application ng new DemoApp
Application live on
Understanding main Building blocks of
Angular application
Components
• @Component Decorator is used to make a typescript class as component
• Angular app can have one or more components.
Templates
• It’s a HTML along with angular expressions
Modules
• Modules has collections of Components & Services
Service
• Services is a typescript class used for business logic and separation of concerns
Understanding Modules
Array used to import other modules into the app module (root module)
{{ expression }}
Used to retrieve values from component to html page (*.ts) to (*.html)
Component.html Component.ts
One way data binding
Other Data Binding approaches
Property binding
[property] = “value”
Component.html Component.ts <input [disabled]="status" type="text"/>
Event binding
(event) = “Handler”
Component.html Component.ts <button (click)="getTitle"> My Button
</button>
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.less']
})
export class AppComponent {
…..
}
Modules
So a module is a collection of related components.
ng g m mymodule
Create component inside
Component.html Component.ts
Calling event handler along
with input values
https://github.com/mkjitlearnings/AllAngular8
Angular – 8 Directives
https://angular.io/guide/attribute-directives
Introduction https://angular.io/guide/lifecycle-hooks
Angular allows us to trigger the actions at the specific point in the lifecycle of components.
Such as
a) Whenever property of component changes. or
b) Any view render or
c) Any component created or destroyed.
5. ngAfterContentInit() 9. ngOnDestroy()
1. Constructor
7. ngAfterViewInit()
3. ngOnInit()
4. ngDoCheck() 8. ngAfterViewChecked()
2. ngOnChange
6. ngAfterContentChecked()
Decorators
Decorators are a design pattern that is used to separate modification or decoration of a class without modifying
the original source code. In AngularJS, decorators are functions that allow a service, directive or filter to be
modified prior to its usage.
1) @NgModule
2) @Component
3) @Injectable
4) @Pipe
Parent Component
On click event data is passing
from parent component to
child component
Child Component
Continue..
Parent Component Child Component
@Output() Decorator
Parent Component
https://angular.io/api/core/EventEmitter
Code https://github.com/mkjitsolution/angular_ComponentInteraction
1) Lowercase
2) Uppercase
3) Titlecase
4) Slice
5) Json
6) Number
7) Percent
8) Currency
9) Date
https://www.concretepage.com/angular-2/angular-2-decimal-pipe-percent-pipe-and-currency-pipe-example
Creation of Custom Pipes
In order to create Custom pipes.
Service Class
1) By Name or
2) Based on Project or
3) Based on salary (1500-3500, return two employees record)
Angular Forms
Developers Task
1) Data Binding
2) Change Tracking
3) Validation
4) Visual Feedback
5) Error Messages
6) Form submission
Template Driven Form (TDF)
Creation of Model
class
Apply
Validations and
associated css.
Add Submit
event
Add form
submit
validations
Note : Make sure Angular CLI Version should be 6 or more
Step 1 : Create Form
Step 1-B : Binding Form With Component
Step 1 : Add form modules in app.module.ts
Discuss about :
ngForm
ngModel
Step 2: Generate Model Class
Account class
Accounts
Service
These required, minlength, maxlength and pattern attributes are already in the official HTML specification.
They are a core part of HTML and we don’t actually need Angular in order to use them.
If they are present in a form then the browser will perform some default validation itself.
Understanding ngModel Validation properties
Source : https://angular.io/guide/forms
Continue…
Property Description
error error object contains all the validation attributes applied to the specified
element.
pristine Returns true if the user has not interacted with control yet else returns false.
dirty Returns true if user changed the value of model at least once
touched Returns true if the user has tabbed out from the control.
untouched Returns true if the user has not tabbed out from the control.
Step 4 : Applying Validations
Step 5 : Submitting the Form
It has Structured data model and form validations are handled through functions.
• Angular FormControl is an inbuilt class that is used to get and set values and validation of the form
control fields like <input> or <select>.
• The FormControl tracks the value and validation status of an individual form control.
For every form control such as text, checkbox, radio button, we need to create the instance of FormControl in
our component.
Component Class & HTML-Template
Git Resource
https://github.com/mkjitsolution/
Reactive_Forms
FormGroup
FormGroup is one of the three fundamental building blocks used to define the forms in Angular, along with
FormControl and FormArray.
The FormGroup aggregates the values of each child FormControl into one object, with each control name as a
key.
https://angular.io/api/forms/FormGroup#description
Git Resource
https://github.com/mkjitsolution/Reactive_Forms
Self Learning
Form Builder & Custom Validation
The FormBuilder is the helper API to build forms in Angular. It provides shortcuts to create the instance of the
FormControl, FormGroup or FormArray. It reduces the code required to write the complex forms.
https://github.com/mkjitsolution/Reactive_Forms
Http Observable
Http Observable use to fetch data from Rest endpoints
The HttpClient in @angular/common/Http offers the simplified client HTTP API for Angular applications that rests
on an XMLHttpRequest interface exposed by browsers.
The Observable isn’t an Angular specific feature, but a new standard for managing async data that will be
included in the ES7 release.
Service
Server DB
Component Component Component
1 2 n
Managing Subscription
We subscribe to the observable ourselves using the actual subscribe() method.
componentProperty: ModelClass;
ngOnInit() {
//we've to manually subscribe to this method and take the data
// in our callback
this. __studentService.callServiceMethod()
.subscribe((feed)=>
{
this.componentProperty = feed ;
})
console.log(" ---- inside http observable component "+this.students.length);
}
Code-Step 1 Adding HttpClientModule in App module
Step 2 – Updating Service
Step 3 : Updating Component & HTML template
Http Template
Assignment
Implement error handling messaging while any error occurs during server call.
Such as , Server 404, 500 errors , bad json error etc.
from
to
We are not getting any output , instead we should get proper error message like “bad file name”
Git Resource
https://github.com/mkjitsolution/HttpObservable
https://blog.angular-university.io/rxjs-error-handling/
https://www.concretepage.com/angular/angular-catcherror
Routing & Navigation
https://angular.io/guide/router https://angular.io/api/router/Route
The Angular router is an essential element of the Angular platform.
The Angular Router enables navigation from one view to the next as users perform application tasks.
So ,
In Angular we have components, each component can have a different task,
for example in Accounts Application there can be a Profile, Balance, Policies , Investment , Insurance views.
ʘ Routing enables a user to visit these pages or components with each one having a specified URL path.
ʘ These URL’s or RouterLinks can be accessed by a user through hyperlinks in HTML templates, javascript’s
navigate methods or by simply pasting in browser’s address bar.
ʘ Through routing we can use the browser's URL to navigate between Angular components in the same way
you can use the usual server side navigation.
Configuration of Router
• A routed Angular application has one singleton instance of the Router service.
• When the browser's URL changes, that router looks for a corresponding Route from which it can determine
the component to display
Router
Path Component
What’s new by choosing Routing
1) app-routing.module.ts
https://angular.io/api/router/RouterOutlet
routerLink attribute The routerLink attribute can be used on any element. It makes that element clickable
in order to activate the specified route. It can be used in a similar way to
the href attribute on links.
https://angular.io/api/router/RouterLink#description
For example
routerLinkActive In order to style router links to the currently active route, the routerLinkActive attribute
is provided. It accepts one or more class names, that will be toggled on the element
when its routerLink points to the active route.
Development Process
Configure Routes/Links
2 with Components
Provide navigation
3 controls
Application
https://css-tricks.com/
Step 1 : Basic Requirements
Step 2 : Configure Routes
https://angular.io/api/router/Routes
https://angular.io/api/router/Route
Step 3 : Navigation Controls
Git Resource
https://github.com/mkjitsolution/
Angular_Routing
www.amazon.com/watch/man/analogwatch/101
https://www.mkj-it-learnings.com/