Skip to content

Instantly share code, notes, and snippets.

View killerchip's full-sized avatar

Costas Ioannou killerchip

  • Morrow Digital
  • Larissa, Greece
View GitHub Profile
@killerchip
killerchip / action.interface.ts
Last active January 21, 2018 21:01
Angular Template Code: A small "home made" redux store for simple/small angular applications.
export interface IAction {
type: number;
payload: any;
}
@killerchip
killerchip / angular-cheat-nested-routes.md
Last active May 1, 2022 14:46
Angular cheatsheet - How to create Angular nested routes

Nested Routes

The following describes how to create and use nested routes in Angular.

Example:

We have an e-shop with two main areas, the Home area and the Special Offers area. In special offers page we have two sub-pages: Today's Offers and Last Pieces offers.

Define which root routes will have child routes

@killerchip
killerchip / angular-cheat-route-guards.md
Last active July 29, 2018 07:55
Angular cheatsheet - Route guards... how to prevent accesss to routes

Route Guards

Route guards are services that implement logic to allow or not fulfilling a route.

generate a guard

You can generate a guard with angular-cli:

> ng generate guard Auth
@killerchip
killerchip / angular-cheat-grab-url-parameters.md
Created January 3, 2018 08:55
Angular cheatsheet - how to grab URL parameters in routing

Grabbing URL parameters

When 'routing' with Angular you can also have the traditional URL parameters like ?category=toys?maxprice=23.

ActivatedRoute can return those as observables.

...
import { ActivatedRoute } from '@angular/router';
@killerchip
killerchip / angular-cheat-route-parameters.md
Last active July 29, 2018 08:09
Angular cheatsheet - Defining and reading route parameters

Route parameters

Defining and reading route parameters

Define the parameter in routes

app.component.ts:

const routes: Routes = [
  {path: 'products/:id', component: ProductsComponent }
];
@killerchip
killerchip / angular-cheat-hash-routring.md
Last active January 6, 2018 20:51
Angular cheatsheet - switching angular routing from default (HTML5 routing) to Hash based routing
@killerchip
killerchip / angular-cheat-basic-routing.md
Last active July 29, 2018 08:10
Angular cheatsheet - Basic routing

Angular Basic Routing

The following is an example on how to setup basic routing

import RouterModule and Routes

app.module.ts:

import { RouterModule, Routes } from '@angular/router';
@killerchip
killerchip / angular-tip-rxjs-launch-series-of-requests.md
Created January 1, 2018 06:57
Angular tip: RxJS - Launch a series of requests one after the other

Launch a series of requests one after the other

In this example we launch a series of requests from information derived from an array. The requests are launched one after the other. This means a "next" request is launched only when the "previous" has been resolved.

The trick here is done by concatMap operator which does exactly what we want. So we create an Observable based on an array of initial values. Then concatMap will render them into a single stream the way we described above.

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';
import { concatMap } from 'rxjs/operators';
@killerchip
killerchip / angular-tip-rxjs-gather-from-apis.md
Created January 1, 2018 06:55
Angular tip: RxJS - Gather up info from APIs

Gather up info from APIs

In this example we gather up information from two different API endpoints, combine them and present them as one object to the subscriber.

Explanation:

  • We create an observable that queries the 1st endpoint.
  • With mergeMap operator we trigger query for the 2nd endpoint. The 2nd query depends on the results of the 1st query.
  • Finally we combineLatest the two observables into one that emits the combination of the two.
@killerchip
killerchip / angular-tip-rxjs-monitor-until.md
Last active January 1, 2018 06:52
Angular tip: RxJS a source until certain condition

Monitor until condition

In this scenario we want to repeatedly monitor an API endpoint, until it retuns a specific value (or condition).

Explanation:

  • We use interval operator to create an observable that emits a dummy event (a counter) on specific intervals.
  • We use these dummy events to trigger calls to remove API, and merge the result back to the stream, with mergeMap.
  • Finally with takeWhile we check if the condition we want is met. If the conidition is met then
    • the particular event is not emitted
    • the transmition stops