Angular Notes
Angular Notes
-------
v1.0 : 2010 (AngularJS) --> JavaScript
v2.0 : 2016 (Angular) --> TypeScript (superset of JavaScript)
v4.0 : 2017
v6.0 : 2018
v8.0 : 2019
Installing Angular
------------------
1) npm install -g @angular/cli@6.0
2) ng -v : To check the Angular version
3) Create a folder for Angular training and navigate to that folder.
CD C:\GlobalLogic-MSNET-Training\Web\Angular
4) Create a new Angular project (example: AngularProj01).
ng new AngularProj01
5) Switch to the new folder.
cd AngularProj01
6) To start the project:
ng serve
7) To test, open a browser and type:
http://localhost:4200
Data Binding
------------
1) Right-click on 'src' folder and select 'New Folder'.
2) Name the folder as 'models'.
3) Right-click on 'models' folder and select 'New File'.
4) Name the file as 'Employee.ts'.
5) Code the class.
6) Open the 'app.component.ts' file from the 'src/app' folder.
7) Import the Employee class.
8) Create an object named 'emp' for the Employee class inside the 'AppComponent'
class.
9) Open the 'app.component.html' file from the 'src/app' folder.
10) Code the HTML and bind the object data to controls using 'ngModel'.
11) Open the 'app.module.ts' file from the 'src/app' folder.
12) Import the 'FormsModule'.
13) Add the FormsModule in 'imports' section of the @NgModule attribute.
Exercise:
--------
* Create a new Angular project to display the details of a product, which include
product id, name, category and price.
Exercise:
--------
* Create an Angular project and add 2 components:
1) employee which displays the details of a single object of Employee class.
2) empList which displays an array of Employee objects.
Pipes (Filters)
-----
1) lowercase
2) uppercase
3) currency
4) titlecase
5) date
6) json
7) percent
ngIf
----
* To display some data conditionally.
ngSwitch
--------
* To check for multiple conditions.
<tag [ngSwitch]="field">
<tag *ngSwitchCase="value1">
tags
</tag>
<tag *ngSwitchCase="value2">
tags
</tag>
<tag *ngSwitchCase="value3">
tags
</tag>
...
<tag *ngSwitchDefault>
tags
</tag>
</tag>
Example:
<ul [ngSwitch]="countryCode">
<li *ngSwitchCase="US">United States of America</li>
<li *ngSwitchCase="IN">India></li>
<li *ngSwitchCase="AU">Australia</li>
<li *ngSwitchDefault>Unknown country</li>
</ul>
Angular Services
----------------
* In an Angular application, components deal with presenting the data to the view.
Fetching data for the components to display is handled by Angular services.
* From the official documentation:
"Components shouldn't fetch or save data directly and they certainly shouldn't
knowingly present fake data. They should focus on presenting data and delegate data
access to a service."
* Another advantage of using Angular services is that it makes it easier to share
data between two separate components.
Creating a Service
------------------
1) Navigate to the project folder.
2) Use the "generate service" command of ng to create a new service.
ng g service myservice
3) We need to include the service created in the main parent app.module.ts.
import { MyserviceService } from './myservice.service';
...
providers: [MyserviceService],
4) In the service class, we will create a function. Open the myservice.service.ts
file.
showTodayDate() {
let ndate = new Date();
return ndate;
}
Exercise:
--------
* Create an Angular service called ArithmeticService with the following methods:
1) Add
2) Subtract
3) Multiply
4) Divide
All the methods take 2 numbers as input and return a number.
Create a component which accepts the 2 numbers in text boxes, and an operator in a
combobox.
On clicking the Calculate button, the result should be displayed.
* Create an Angular project with Product class. Create a service to perform CRUD
operations on an array of products.
1) addProduct(prod: Product)
2) getAllProducts(): Product[]
3) getProductById(pid: number): Product
4) updateProduct(pid: number, newprod: Product)
5) deleteProduct(pid: number)
* Create a component to use the above service.
HTTP Service
------------
* Http Service will help us fetch external data, post to it, etc.
* We need to import the http module to make use of the http service.
1) Install rxjs package.
npm install -g rxjs@6 rxjs-compat@6 --save
2) Open the app.module.ts file.
import { HttpModule } from '@angular/http';
...
imports: [
...,
HttpModule
],
3) Use the Http service in the component's class.
import { Http } from '@angular/http';
import { map } from 'rxjs/Operators';
...
constructor(private http: Http) { }
ngOnInit() {
this.http.get("http://jsonplaceholder.typicode.com/users").
pipe(map((response: any) => response.json())).
subscribe(result => { console.log(result);});
}
4) Two operations are performed on the fetched url data: map and subscribe. The Map
method helps to convert the data to json format. Once the map is done, the
subscribe will log the output in the console.
HTTP Methods
------------
url = "http://jsonplaceholder.typicode.com/users"
Routing
-------
* We access our application through one URL such as http://localhost:4200 and our
application is not aware of any other URLs such as http://localhost:4200/todos.
* Most web applications need to support different URLs to navigate users to
different pages in the application. That�s where a router comes in.
* Angular router is a JavaScript router implementation that�s designed to work with
Angular and is packaged as @angular/router.
Implementing Navigation
-----------------------
1) Create a new component called Home.
2) Open the home.component.html file.
3) Add the following code to it:
<h2>
Welcome to Home Page
</h2>
<nav>
<a routerLink="/xxx" routerLinkActive="active">Go to xxx</a>
</nav>
4) Import the component in app.module.ts file.
import { HomeComponent } from './home/home.component';
5) Add the HomeComponent to the NgModule in the app.module.ts file.
@NgModule({
declarations: [
AppComponent,
CalcComponent,
HomeComponent
], ...
6) Add the following code to the app.component.html file:
<router-outlet></router-outlet>
7) Import the component in app.routing.ts.
import { HomeComponent } from './home/home.component';
8) Inside the app.routing.ts file, include the default route to display as the
HomeComponent.
{ path: '', component: HomeComponent }
9) To handle undefined routing requests, add the following:
{ path: '**', component: NotFoundComponent }
10) Create the NotFound component.
11) Display some error message in the NotFound component's HTML file.
12) Save the above changes and restart the Angular application using ng serve.
13) Point your browser to http://localhost:4200/xxx and you will have the
xxxComponent displayed.