Angular 5 is a framework for building client applications in HTML and either JavaScript or a language like TypeScript that compiles to JavaScript.
The framework consists of several libraries, some of them core and some optional.
You write Angular applications by composing HTML templates with Angularized markup, writing component classes to manage those templates, adding application logic in services, and boxing components and services in modules.
Then you launch the app by bootstrapping the root module. Angular takes over, presenting your application content in a browser and responding to user interactions according to the instructions you've provided.
Install command line utility cli:
npm install -g @angular/cli
Create new project
ng new myAngular5App
Inside the root folder:
Start project:
ng serve --open
run unit tests:
ng test
run integration tests:
ng e2e
Create component:
ng generate component heroes
Create service in the app module:
ng generate service message --module=app
=> will add this line: 'providers: [ HeroService ],' in src/app/app.module.ts (providers).
Create routing module: (details)
ng generate module app-routing --flat --module=app
--flat puts the file in src/app instead of its own folder.--module=app tells the CLI to register it in the imports array of the AppModule.import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HeroesComponent } from './heroes/heroes.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { HeroDetailsComponent } from './hero-details/hero-details.component';
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'heroes', component: HeroesComponent },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'detail/:id', component: HeroDetailsComponent },
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
routed view goes here:
<h1>{{title}}</h1>
<nav>
<a routerLink="/dashboard">Dashboard</a>
<a routerLink="/heroes">Heroes</a>
</nav>
<router-outlet></router-outlet>
<!-- Routed views go here -->
<app-messages></app-messages>
Import the following elements to have access to the routing inside your component.
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
you can then retrieve parameters using:
const id = +this.route.snapshot.paramMap.get('id');
you can navigate back to the parent form by:
goBack(): void {
this.location.back();
}
HttpClient
Http methods return one value
All
HttpClient methods return an RxJS Observable of something.
HTTP is a request/response protocol. You make a request, it returns a single response.
In general, an
Observable can return multiple values over time. An Observable from HttpClient always emits a single value and then completes, never to emit again.
This particular
HttpClient.get call returns an Observable<Hero[]>, literally "an observable of hero arrays". In practice, it will only return a single hero array.
HttpClient.get returns response data
HttpClient.get returns the body of the response as an untyped JSON object by default. Applying the optional type specifier, <Hero[]> , gives you a typed result object.
The shape of the JSON data is determined by the server's data API. The Tour of Heroes data API returns the hero data as an array.
private searchTerms = new Subject<string>();
// Push a search term into the observable stream.
search(term: string): void {
this.searchTerms.next(term);
}
A
Subject is both a source of observable values and an Observable itself. You can subscribe to a Subject as you would any Observable.
You can also push values into that
Observable by calling its next(value) method as the search() method does.this.heroes$ = this.searchTerms.pipe(
// wait 300ms after each keystroke before considering the term
debounceTime(300),
// ignore new term if same as previous term
distinctUntilChanged(),
// switch to new search observable each time the term changes
switchMap((term: string) => this.heroService.searchHeroes(term)),
);
debounceTime(300)waits until the flow of new string events pauses for 300 milliseconds before passing along the latest string. You'll never make requests more frequently than 300ms.
distinctUntilChangedensures that a request is sent only if the filter text changed.
switchMap()calls the search service for each search term that makes it throughdebounceanddistinctUntilChanged. It cancels and discards previous search observables, returning only the latest search service observable.
--
No comments:
Post a Comment