Angular Notes
Angular Notes
Angular Packages
1. @angular/core
• Provides essential pre-defined decorators, classes, interfaces and modules that are
needed to run every angular application.
• Ex: @Component, @NgModule, @Pipe,@Directive, @Injectable, @Inject, NgZone,
OnChanges,OnInit, Application Module etc.
2.@angular/common
Provides built-in directives that are useful for most of the real-time applications.
• Ex:nglf, ngSwitch, ngClass, ngFor etc.
3.@angular/compiler
• Compiles "templates" (html code) into "javascript code".
4.@angular/platform-browser-dynamic
• Invokes the angular compiler (JIT compilation) and specifies hathe startup module
and also start executing the application.
5.@angular/platform-browser
• Provides a set of pre-defined classes that are related to DOM and browser
interaction.
• Ex:BrowserModule.
6.@angular/forms
Provides necessary pre-defined classes that are related to are needed to create and
execute angular forms.
• Ex: FormsModule, ReactiveFormsModule, Validators,ngModel, ngForm etc.
7.@angular/router
• Provides necessary pre-defined classes that are needed to create and execute
angular routes.
Ex: RouterModule, Routes, Activated Route, CanActivate, routerlink etc.
8.@angular/animations
• Provides necessary pre-defined classes that are needed to create and execute
angular animations.
• Ex:BrowserAnimations Module, animate,state, style,transition etc.
9.@angular/cli
Provides necessary pre-defined commands that are needed to create, compile, build,
add items in angular applications.
Ex:ng new,ng serve, ng build, ng test etc.
10. rxjs
Provides necessary pre-defined classes for creating Observables, which are needed
to represent the response of REST-API calls of AJAX.
.Ex:Observable,Observer, Subject etc.
11. zone.js
Provides necessary pre-defined classes for executing "change detection processes",
while executing angular app.
WHAT IS COMPONENTS
Component is a class that contain "application data + even handler methods".
For every screen, you require to create a component.
For example for a dashboard page you require to create a dashboard component.
Component contains application data in the form of properties or arrays, that is
required to display the view.
And also it contains even handler methods, in order to respond to user actions.
For example in the "myprofile" page, I want to display the name of the person.
So person name is the property that is available in the component class.
And when the user clicks on the save button, it is going to execute a method called
"onSaveClick", which is present inside the component class.
Every component is associated to a template.
In other words, one component has only one template.
Template contains design logic that is HTML logic.
Template can access all the properties and event handler methods of the
corresponding component.
For example the text box binds to "personName" property of the component.
The button binds to "onButtonClick" method of the component.
A component can call other component through its template.
That means in the template of one component, you can call another component.
Component's additional details are called metadata of the component.
What is routing?
"Angular Routing" is an integral part of Angular, which allows us to load components
based on the URL changes in the browser's location bar.
So the user experiences the page navigation using single page application technique.
Here Single page application is a technique, which allows us, to navigate among
screens only within the client side itself, instead of reloading the full page.
A Route maps an URL to a component.
For example the URL dashboard, maps to dashboard component.
So when another user enters the URL "dashboard", it automatically loads the
dashboard component.
1 Define Base URL in "index.html"file
<base href="/">
Data Bindings
Event Binding
Component Template
()
Property HTML Element
Two-Way Binding
Component Template
[( )]
Property HTML Element
Style Handling
• [style.property] = "value"
• [style.property] = "(condition)? truevalue : falsevalue"
• [ngClass] = "value"
• [ngClass] = "(condition)?truevalue:falsevalue"
Built-in Pipes
Component Template
Pipe
ngSwitch
<tag[ngSwitch]="property">
</tag>
•Mainly used to organize the components and others (directives and pipes).
Goals of Modules:
• Make some of the components, directives, pipes public; so that, other module's
component templates can use them.
•Import components, directives, pipes from other modules, that are required by
current module's component templates.
•An NgModule is a class that is decorated with "NgModule" decorator, that contains
the following metadata;
Module metadata:
• declarations: List of components, directives and pipes, that are part of current
module.
•exports: List of components, directives, and pipes, that are public, that can be
accessible in other modules, that are importing the current module.
•imports: List of modules, that the current module imports; so, the current module
can use components, directive, pipes that are already exported by that particular
module.
• providers: List of services that can be used by the components, directives and
pipes of current module.
▪ Make components contain code for only supplying the data to the template and
respond to the user actions such as click and also call the necessary services.
• Create a class with one or more properties and methods that contains business
logic and data access logic.
"subscriber".
Flow of functionality:
• Observable is created.
What is AJAX?
• AJAX(Asynchronous JavaScript And Xml) is NOT a language, but it is a
concept,which is used to"send background request to server" and also "get
background response from server",without refreshing (reloading) the web page in
the browser.
Browser(Client)
Server
• User performs some activity
Receive the request from browser
Send AJAX request to server
• Do process(db connection)
Same Browser(Client)
↓
Get response from server
Send response to browser
Display the response
Types of Requests
RXJS Map
pipe(map(
(data) =>
return data;
))
JWT Authentication
• A JSON Web Token (JWT) is a JSON object that is defined in RFC 7519 as a safe
way to represent a set of information between two parties.
.Once the user logs-in, JWT(an encrypted token) will be sent to the client;
• Each subsequent request includes JWT sent to the server;then the server
validates JWT and provides response only when the JWT token is valid.
.The signature is generated based on the header and payload; so that receiver can
verify the content hasn't been tampered with.
• required attribute
• minlength attribute
• maxlength attribute
• untouched property
• touched property
• prestine property
• dirty property
• valid property
• invalid property
• errors property
<div class="col-sm-8">
<input type="text"id="txtNewProjectID"style="width:130px"class="form-
control"placeholder="Project
ID"name="ProjectID" [(ngModel)]="newProject.projectID"
required="required"pattern="^[0-9]*$"
#newProjectID="ngModel"[ngClass]="{"is-invalid":newProjectID.invalid
&&(newProjectID.dirty ||
newProject.touched || newForm.submitted)}">
errors? - we are using "?", which indicates that read the right side property i.e. "required"
only when the "errors" property is != null.
Reactive Forms
• Suitable for simple/complex forms with any no. of fields.
• Unit testable.
<form [formGroup]="myForm">
<input type="text"formControlName="personName">
</form>
1 ths.myForm.reset();
• You can add FormControl, FormGroup or another FormArray, inside the FormArray.
• Validators.pattern function
• Validators.minlength function
• Validators.maxlength function
● Validators.min function
● Validators.max function
● Validators.email function
if(condition)
else
};
class ChildComponent
@Input("someInputName"): propertyName:dataType;
class ParentComponent
//If we want to access any child component method from parents component.
template.
class ParentComponent
{
@ViewChildren("referenceVariable"): variableName :QueryList<ChildComponent>;
class ChildComponent
eventName(event) {
this.someOutputName.emit({event})
--parents component
<app-child(someOutputName)="methodName()"> </app-child>
//If we want to access any method of parent components from child component then
use output decorator
});
myObservable.subscribe((data)=>{
});
RXJS - Subject
subject.next(data);
mySubject.subscribe((data) => {
});
2. Next, render the content, inside the child component's template, using <ng-
content>.
<app-child>
<element1></element1>
<element2></element2>
</app-child>
<ng-content> </ng-content>
CONTENTCHILD
The child component can access the properties and methods of grandchildren that are
passed by the parent component, by using the ContentChild decorator.
1. ParentComponent supply some grand children elements, as "content", to the
ChildComponent.
<app-child>
</app-child>
Child Component
class ChildComponent
Custom Pipe
.Pipe class is decorated with @Pipe decorator.
@Pipe({
name: 'pipeName'
})
return yourTransformedValue;
Pure Pipe
@Pipe({
name:'pipeName'
})
• Pure pipe doesn't re-executes, in case of any changes to the object properties.
• Pure pipe doesn't re-executes, in case of any elements added to removed in the
array.
Inpure Pipe
@Pipe({
name:'pipeName',
pure:false
})
Interview Question