Introduction to Angular
Platform for building client applications with web technology
HTML, JavaScript/TypeScript, CSS/SASS
Angular covers (read more details ):
Modules
Components (=application logic)
Templates (=view)
Data Binding (Components to Templates )
Services
Dependency Injection
npm install -g @angular/cli
ng help
ng new my-first-project
cd my-first-project
ng serve
Syntax
Description
(click)="onSave()"
One-way event binding from element to component (event binding )
<input [(ngModel)]="firstName">
Two-way binding (ngModel )
For two-way binding you need the FormsModule
...
import { FormsModule } from '@angular/forms'
...
@NgModule({
...
imports: [
..., FormsModule, ...
], ...
})
export class AppModule { }
Define HTML layout and structure
Prefixed with *
Syntax
Description
*ngFor="let i of items"
Repeater directive (ngForOf )
*ngIf="len > 3"
Conditional display (ngIf )
ngSwitch
/*ngSwitchCase
Conditional display (example )
Syntax
Description
{{ birthday
| date:'longDate'}}
Pipe operator
Advanced Template & Data Binding
...
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [ ..., HttpClientModule, ... ],
declarations: ...,
bootstrap: ...
})
export class AppModule {}
Get instance of HttpClient
in constructor (Dependency Injection )
...
import { HttpClient } from '@angular/common/http';
@Component(...)
export class MyComponent {
constructor(private http: HttpClient) { ... }
...
}
<!--#include file="angular/0020-http-client/app.component.ts" -->
<!--#include file="angular/0020-http-client/app.component.html" -->
Angular supports navigating between client-side views using the URL
Support passing parameters in the URL
base
element defines the base URL for all relative URLs
Uses HTML5's push state functionality in the background
Can be changed to hash URLs for older browsers if necessary
See also LocationStrategy
Generate new Angular app including router with --routing
switch in CLI
Example: ng new <your app name> --routing
<!--#include file="angular/0040-routing/src/app/app-routing.module.ts" -->
Add Routing Module to Main Module
<!--#include file="angular/0040-routing/src/app/app.module.ts" -->
<!--#include file="angular/0040-routing/src/app/welcome.component.ts" -->
Processing Route Parameters
<!--#include file="angular/0040-routing/src/app/customer-details.component.ts" -->
<!--#include file="angular/0050-ng-flex/src/app/simple-layouts/simple-layouts.component.html" -->
<!--#include file="angular/0050-ng-flex/src/app/ordering/ordering.component.html" -->
Web apps get important features of desktop apps
Examples:
Runs offline, too
Push notifications
Homescreen icon
Work on different screen sizes (response design )
Learn more:
Started in version Angular 5 and Angular CLI 1.6
Supported by Angular CLI
--service-worker
switch for ng new
command
Generates "serviceWorker": true
option in .angular-cli.json
Adds the @angular/service-worker
NPM package
Calls ServiceWorkerModule.register
in app.module.ts
Creates the ngsw-config.json configuration file
Only active for production build
--prod
switch in ng build
command
For results, see dist folder
Use http-server
NPM package or Chrome Development Webserver to test
Angular Service Worker Configuration
{
"index" : " /index.html" ,
"assetGroups" : [... ],
"assetGroups" : [... ],
"dataGroups" : [... ]
}
Further Readings and Exercises
Want to know more? Read/watch...
Exercises