Angular
Angular
it)
http://www.dais.unive.it/~bergamasco/
DAIS - Università Ca’Foscari di Venezia
Anno accademico: 2017/2018
Angular (conosciuto anche come Angular 2 o
Angular 5) è un framework front-end per lo sviluppo
di client web SPA
● Sviluppato da Google (Angular team)
● Open-source
● Basato su TypeScript
● Modulare
Database
Client browser
Database
Client browser
@sealed
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
F. Bergamasco - WEB APPLICATIONS AND TECHNOLOGIES - CT0142
I building-blocks di un’app Angular
sono chiamati NgModules che
fungono da contenitori di
componenti, service providers, e tutto
il codice accomunato da un certo
dominio, workflow o features
strettamente legate
@NgModule({
imports: [ BrowserModule ],
providers: [ Logger ],
declarations: [ AppComponent ],
exports: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
@NgModule({
imports: [ BrowserModule ],
providers: [ Logger ],
declarations: [ AppComponent ],
exports: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { } Angular module
View
@Component({
selector: 'app-root',
<div style="text-align:center">
templateUrl: './app.component.html',
<h1>
styleUrls: ['./app.component.css']
Welcome to {{ title }}
})
</h1>
export class AppComponent {
</div>
title = 'app';
}
app.component.ts
app.component.html
F. Bergamasco - WEB APPLICATIONS AND TECHNOLOGIES - CT0142
Le views possono essere strutturate in una gerarchia
(una view può includere altre view). Una child-view
può appartenere allo stesso modulo o ad un altro
NgModule
@Component({
selector: 'app-hero-birthday',
template: `<p>The hero's birthday is {{ birthday | date }}</p>`
})
<div *ngIf="expr"></div>
<ul>
<li *ngFor="let product of get_products()">
{{ product }}
</li>
<ul>
doSequence(seq, 0);
sequenceObservable.subscribe(
(x) => { console.log(“New value generated: “+x); },
(err) => { console.log(“Error: “ + err );
() => { console.log(“No more values”);
);
// Logs
// 1 4 9
toggle() {
this.visible = !this.visible; if (this.visible) { this.visibility_change.emit(true); }
else { this.visibility_change.emit(false); }
}
} F. Bergamasco - WEB APPLICATIONS AND TECHNOLOGIES - CT0142
E una delle classi più utilizzata di Angular.
Permette di effettuare chiamate HTTP asincrone e di
ritornare i valori ottenuti sotto forma di observable.
Observable<Users[]> o = http.get<Users[]>(‘http://myserver.com/api/v1/users)
.pipe(
tap(users => this.log(`fetched users: ‘+JSON.stringify(users) )),
catchError(this.handleError('getHeroes', []))
);
2. Effettuare la richiesta
http.get( <url>:string, <options>:{} ) : Observable< any >
<base href="/">
Nome modulo
Importa automaticamente il
nuovo modulo nel modulo
Non generare una dir “app” (solitamente è il nome
dedicata per il modulo del root module)
F. Bergamasco - WEB APPLICATIONS AND TECHNOLOGIES - CT0142
Si specificano le route istanziando un oggetto Routes
https://angular.io/api/router/Routes
const routes: Routes = [
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: UserLoginComponent },
{ path: 'messages', component: MessageListComponent }
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule { }
F. Bergamasco - WEB APPLICATIONS AND TECHNOLOGIES - CT0142
Si invoca il metodo forRoot() che ritorna un modulo con tutti i servizi
e direttive necessarie per il suo utilizzo. Il modulo è configurato con
le route appena definite ed esportato agli altri componenti
dell’applicazione
<h1>{{title}}</h1>
<router-outlet></router-outlet>
<app-messages></app-messages>