01 Part1 Recap
01 Part1 Recap
Recap
1
Environment setup: install (node and npm)
• Node.js:
− open source server framework
− run JavaScript outside the browser, in the JavaScript engine of Node.js itself
− contains a built-in HTTP server => possible to run a web server without Apache
− download: https://nodejs.org/en/download/
• NPM:
− Node.js Package Manager
− Package or module = JavaScript library
− with NPM: install necessary packages
2
Environment setup: install angular CLI
npm install –g
@angular/cli
• This will install the Angular CLI (may take a while)
• "-g" stands for "global mode" and ensures that it is
not installed in the local directory
• at least one global version of Angular CLI is needed
3
Environment setup: create new project
4
Project structure
5
Environment setup: add bootstrap
• In cmder enter the following command:
cd [ToWorkingDir]
npm install @popperjs/core bootstrap -- package.json
save
6
Environment setup: add bootstrap
• define bootstrap module path in angular.json (root project)
• @styles: "./node_modules/bootstrap/dist/css/bootstrap.min.css",
Order!!
• @scripts:
"./node_modules/@popperjs/core/dist/umd/popper.min.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js" "
Order!!
• restart ng serve to make sure the changes are taken into account
7
app starting files: MAIN.TS
• bootstrapApplication:
− starting instance of application (AppComponent)
− extra options (appConfig), needed for DI (see later)
GUI design
8
app starting files: app.config.ts
• providers:
− contains an array of services
− app.routes.ts => contains routing
GUI design
9
Architecture angular app
app module
app
component
10
Component
11
Lifecycle hooks
Called before ngOnInit if the component has bound inputs and after a bound input
ngOnChanges property changes
ngOnInit Called once the component is initialized (only after first ngOnChanges if any!)
ngAfterContentInit Called after content (ng-content) has been projected into view (only once)
ngAfterContentChecked Called every time the projected content has been checked
ngAfterViewInit Called after the component’s view (and child views) has been initialized
ngAfterViewChecked Called every time the view (and child views) have been checked
12
Parent – child communication
<child (outputEvent)="function()" …
></child>
• ng-content + @contentchild
13
Services
− siblings
− components in different hierarchies component
− with backend
ng g s
[ServiceName]
14
Hierarchical Injector
Parent Parent
(no service) (service A)
Child 1 and 2 both create the same service via DI but Parent now creates the service via DI. Child 1 and 2
receive different instances of this service (A and B) via now receive the same instance of the service from the
the injector. injector (A only) as well as the grandchildren.
Routes
H
om
A
e
bo
ut
C
on
ta
tc
16
Define and register routes, render
component
app.routes.ts: app.config.ts:
Define routes: Register routes:
const appRoutes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component:
AboutComponent },
{ path: 'contact', component:
ContactComponent }
];
17
Navigation
• Via links
<li routerLinkActive="active">
<a routerLink="/servers">Servers</a>
</li>
• Programmatic
this.router.navigate(['/servers']);
18
Routes with parameters
{path: 'books/:isbn', component: ServerCompon
⋮ ent}
import { ActivatedRoute, Params } from '@angular/router';
⋮
isbn = '';
constructor(private route: ActivatedRoute) { }
ngOnInit(): void { Observable
this.route.params
ES6 arrow function:
.subscribe((params: Params) => {
"inline" function with
this.isbn = params['isbn']; what to do when there is
} an event of the
); Observable
}
⋮ "subscribe" to events of the Observable
19
Routes with query parameters
20
Child routes
in parent component:
<router-outlet></router-
outlet>
21
Wildcard and Redirect routes
22