Angular 7 201 300
Angular 7 201 300
• The service is a class contains re-usable code (business logic, validations, calculations etc.), which can be
called in one or more components. If you place the re-usable set of properties and methods as a service
class, it can be called from any component or any other service in the entire application.
• We must decorate the service class with “@Injectable()” decorator, to make the service accessible from any
other component. You can import “@Injectable” decorator from “@angular/core” package.
• We must use “@Inject()” decorator, to request angular to create an object for the service class. Then the
angular framework will automatically creates an object for the service class and passes the object as
argument for your component’s constructor; you can receive it into a reference variable in the constructor.
You can use “@Inject” only in the constructor of component. To make the reference variable as member of
the component class, add “private” or “public” keyword at left side of the reference variable in the
constructor.
• In realtime, all the CRUD operations (Ajax calls) are created in the service; the same is called in the
component class, whenever required.
• Create Service:
@Injectable( )
class Serviceclassname
Methods here
class ComponentClassname
class ModuleClassname
@Component( { … } )
class ComponentClassname
Services - Example
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
ng g class User
ng g service Login
c:\angular\app1\package.json
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\user.ts
c:\angular\app1\src\app\login.service.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
ng serve
http://localhost:4200
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
ng g component India
ng g component Usa
ng g service Population
c:\angular\app1\package.json
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\population.service.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
c:\angular\app1\src\app\india\india.component.ts
c:\angular\app1\src\app\india\india.component.html
c:\angular\app1\src\app\usa\Usa.component.ts
c:\angular\app1\src\app\usa\Usa.component.html
ng serve
• Directive is a class, that can be invoked (called) through an attribute of a tag in the template.
• Directive provides additional functionality for the html element.
• For example, "ngIf" directive checks the condition, displays the element if the condition is TRUE; and
removes the element if the condition is false.
• The "ElementRef" class represents the element, in which the directive is invoked.
• Directive can receive values from the element using @Input() decorator.
• Directive can add events to the element by using @HostListener() decorator.
• We can communicate between the component to the directive, using @ViewChild decorator in the
component.
class directiveclassname
@HostListener("eventname")
methodname( )
class moduleclassname
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
ng g directive Sample
c:\angular\app1\package.json
c:\angular\app1\src\styles.css
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
c:\angular\app1\src\app\sample.directive.ts
ng serve
Pipes - Example
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
c:\angular\app1\package.json
c:\angular\app1\src\styles.css
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
ng serve
Custom Pipes
• Custom pipes are the user-defined pipes.
• Custom pipe must be a class that has @Pipe( ) decorator and implements “PipeTransform” interface.
• The “PipeTransform” interface has “transform” method, which must be implemented in your pipe class.
• The “transform” method will be executed automatically, when the pipe is invoked in the expression
(through pipe ( | ) symbol).
• The “transform” method receive the input value as argument, do process, and return the result value, which
will be displayed in the output.
Syntax to call pipe: {{ property | pipe }}
class moduleclassname
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
ng g pipe Duration
c:\angular\app1\package.json
c:\angular\app1\src\styles.css
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
c:\angular\app1\src\app\duration.pipe.ts
ng serve
Validation Properties:
o untouched
o touched
o pristine
o dirty
o valid
o invalid
o Firstname
o Lastname
o Email
o Amount
o Gender
o Country
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
c:\angular\app1\package.json
c:\angular\app1\src\styles.css
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
ng serve
Validation Properties:
o untouched
o touched
o pristine
o dirty
o valid
o invalid
Fields:
o Firstname
o Lastname
o Email
o Amount
o Gender
o Country
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
c:\angular\app1\package.json
c:\angular\app1\src\styles.css
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
ng serve
o http://localhost:8080/about ➔ AboutComponent
• Create routes:
var variable1 : Routes = [
{ path: “path here”, component: ComponentClassName },
{ path: “path here”, component: ComponentClassName },
….
];
Routing - Example
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
ng g component Home
ng g component About
ng g component Contact
c:\angular\app1\package.json
c:\angular\app1\src\styles.css
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
c:\angular\app1\src\app\home\home.component.ts
c:\angular\app1\src\app\home\home.component.html
c:\angular\app1\src\app\about\about.component.ts
c:\angular\app1\src\app\about\about.component.html
c:\angular\app1\src\app\contact\contact.component.ts
c:\angular\app1\src\app\contact\contact.component.html
ng serve
http://localhost:4200
Click on “About”.
Click on “Contact”.
Route Parameters
• You can pass parameters to the route.
• Route parameter is represented as “:parametername” syntax.
• You can get the value of the parameter in the component using “ActivatedRoute” service.
this.route.snapshot.params["parametername"]
this.route.params.subscribe(params =>
params["parametername"]
});
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
ng g component Home
ng g component About
ng g component Contact
ng g component Products
ng g class Product
ng g service Products
c:\angular\app1\package.json
c:\angular\app1\src\styles.css
c:\angular\app1\src\product.ts
c:\angular\app1\src\products.service.ts
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
c:\angular\app1\src\app\home\home.component.ts
c:\angular\app1\src\app\home\home.component.html
c:\angular\app1\src\app\about\about.component.ts
c:\angular\app1\src\app\about\about.component.html
c:\angular\app1\src\app\contact\contact.component.ts
c:\angular\app1\src\app\contact\contact.component.html
c:\angular\app1\src\app\products\products.component.ts
c:\angular\app1\src\app\products\products.component.html
ng serve
Click on “Apple”.
Click on “Google”.
Child Routes
• Route can have child routes up to unlimited no. of nested levels.
• Ex: "Funds Transfer" menu has "Transfer", "Add Payee", "Activate Payee" etc.
]}
• Create router outlet for child routes (in the parent route component's template):
<router-outlet> </router-outlet>
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
ng g component OnlineShopping
ng g component Appliances
ng g component Electronics
ng g component Fashion
ng g component Furniture
ng g component Lighting
ng g component Mobiles
ng g component Laptops
ng g component Men
ng g component Women
ng g component Furniture
c:\angular\app1\package.json
c:\angular\app1\src\styles.css
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
c:\angular\app1\src\app\online-shopping\online-shopping.component.ts
c:\angular\app1\src\app\online-shopping\online-shopping.component.html
c:\angular\app1\src\app\appliances\appliances.component.ts
c:\angular\app1\src\app\appliances\appliances.component.html
c:\angular\app1\src\app\electronics\electronics.component.ts
c:\angular\app1\src\app\electronics\electronics.component.html
c:\angular\app1\src\app\fashion\fashion.component.ts
c:\angular\app1\src\app\fashion\fashion.component.html
c:\angular\app1\src\app\furniture\furniture.component.ts
c:\angular\app1\src\app\furniture\furniture.component.html
c:\angular\app1\src\app\lighting\lighting.component.ts
c:\angular\app1\src\app\lighting\lighting.component.html
c:\angular\app1\src\app\mobiles\mobiles.component.ts
c:\angular\app1\src\app\mobiles\mobiles.component.html
c:\angular\app1\src\app\laptops\laptops.component.ts
c:\angular\app1\src\app\laptops\laptops.component.html
c:\angular\app1\src\app\men\men.component.ts
c:\angular\app1\src\app\men\men.component.html
c:\angular\app1\src\app\women\women.component.ts
c:\angular\app1\src\app\women\women.component.html
ng serve
Click on “Electronics”.
Click on “Mobiles”.
• The Guard is a service that executes at the specified situation while angular is navigating from one route to
another route.
• Angular mainly supports two types of Guards:
o CanActivate: Executes before entering into a route.
CanActivate
• The "CanActivate" Guard executes before entering into a route.
• Process: User clicks on the hyperlink → Identify the route → CanActivate Guard → Navigate to the Route
→ Corresponding component.
• This guard can be created by implementing "CanActivate" interface.
• The "CanActivate" interface has a method called "canActivate". This method must return a boolean value,
which indicates whether the route can be navigated or not. If we return "true", the route will be navigated;
if we return "false", the route navigation will be stopped.
• It can receive an argument of "ActivatedRouteSnapshot" type, which represents the current state of the
route.
class moduleclassname
CanActivate - Example
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
ng g component OnlineShopping
ng g component Appliances
ng g component Electronics
ng g component Fashion
ng g component Furniture
ng g component Lighting
ng g component Mobiles
ng g component Laptops
ng g component Men
ng g component Women
ng g component Furniture
ng g component Login
ng g service LoginStatus
ng g service LoginAuth
c:\angular\app1\package.json
c:\angular\app1\src\styles.css
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\login-status.service.ts
c:\angular\app1\src\app\login-auth.service.ts
c:\angular\app1\src\app\login\login.component.ts
c:\angular\app1\src\app\login\login.component.html
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
c:\angular\app1\src\app\online-shopping\online-shopping.component.ts
c:\angular\app1\src\app\online-shopping\online-shopping.component.html
c:\angular\app1\src\app\appliances\appliances.component.ts
c:\angular\app1\src\app\appliances\appliances.component.html
c:\angular\app1\src\app\electronics\electronics.component.ts
c:\angular\app1\src\app\electronics\electronics.component.html
c:\angular\app1\src\app\fashion\fashion.component.ts
c:\angular\app1\src\app\fashion\fashion.component.html
c:\angular\app1\src\app\furniture\furniture.component.ts
c:\angular\app1\src\app\furniture\furniture.component.html
c:\angular\app1\src\app\lighting\lighting.component.ts
c:\angular\app1\src\app\lighting\lighting.component.html
c:\angular\app1\src\app\mobiles\mobiles.component.ts
c:\angular\app1\src\app\mobiles\mobiles.component.html
c:\angular\app1\src\app\laptops\laptops.component.ts
c:\angular\app1\src\app\laptops\laptops.component.html
c:\angular\app1\src\app\men\men.component.ts
c:\angular\app1\src\app\men\men.component.html
c:\angular\app1\src\app\women\women.component.ts
c:\angular\app1\src\app\women\women.component.html
ng serve
Click on “Electronics”.
CanDeactivate
• The "CanDeactivate" Guard executes before leaving from a route.
• This guard can be created by implementing "CanDeactivate" interface.
• The "CanDeactivate" interface has a method called "canDeactivate". This method must return a boolean
value, which indicates whether the route can be leave or not. If we return "true", the route will be left; if we
return "false", the route navigation will be stopped.
• It can receive an argument of an user-defined interface type, , which represents the current component.
interface interfacename
canNavigate: boolean;
class moduleclassname
CanDeactivate - Example
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
cd c:\angular\app1
ng g component Home
ng g component About
ng g component Contact
ng g class CanComponentDeactivate
ng g service CanDeactiveGuard
c:\angular\app1\package.json
c:\angular\app1\src\styles.css
c:\angular\app1\src\app\can-component-deactivate.ts
c:\angular\app1\src\app\can-deactivate-guard.service.ts
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
c:\angular\app1\src\app\home\home.component.ts
c:\angular\app1\src\app\home\home.component.html
c:\angular\app1\src\app\about\about.component.ts
c:\angular\app1\src\app\about\about.component.html
c:\angular\app1\src\app\contact\contact.component.ts
c:\angular\app1\src\app\contact\contact.component.html
ng serve
Click on “OK”.
Click on “Home”.
Click on “About”.
Deployment to Java
startup.bat
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
c:\angular\app1\package.json
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
ng build --prod
ng serve
Deployment to .NET
Creating Application
• Open Command Prompt and enter the following commands:
cd c:\angular
ng new app1
c:\angular\app1\package.json
c:\angular\app1\src\app\app.module.ts
c:\angular\app1\src\app\app.component.ts
c:\angular\app1\src\app\app.component.html
ng build --prod
ng serve
• AJAX (Asynchronous JavaScript And Xml) is not a language, but it is a “concept”, which is used to “send a
background request from browser to server” and also “get background response from server to browser”,
without refreshing (reloading) the web page in the browser.
• AJAX allows us to interact with the server and get some data from server, without refreshing the full web
page.
• Ex: Facebook like button, comments, IRCTC search trains.
Advantages of AJAX
• Executes faster
• Less burden on browser (client) and server
• Better user experience.
“@angular/common/http” package
• The “@angular/common/http” package provides necessary services to send ajax request to server and get
ajax response from server.