Skip to content

Instantly share code, notes, and snippets.

View hollygood's full-sized avatar

Kathy H. hollygood

View GitHub Profile
@hollygood
hollygood / angular6-unsubscribe.md
Last active May 17, 2019 14:31
Different ways to unsubscribe Angular 6 Observables

Use 'unsubscribe':

export class ExampleComponent implements OnInit, OnDestroy {
  exampleSubOne: Subscription;
  exampleSubTwo: Subscription;

  constructor(private exampleService: ExampleService) {}

  ngOnInit() {
@hollygood
hollygood / dynamic-fields.ts
Last active November 27, 2018 18:17
Angular 6 Generate Reactive Form Fields dynamically
//========================================= dynamic-fields.service.ts
import { Injectable } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';
/**
* @description Dynamic fields service is used to generate reactive form group fields with Validators
*/
@Injectable({
providedIn: 'root'
@hollygood
hollygood / mutiple-http-streams-with-rxjs.ts
Created December 13, 2018 16:49
Mutiple Http streams with RxJS
/**
* @function forkJoin multiple requests with ngrx selects
* we can't use forkJoin for ngrx selects because this method requires the Observables being 'complete'
* in case of ngrx store, all of their observables are usually based on a BehaviorSubject, and will never complete
* the only way we can do is using like this: forkJoin(user$.take(1), customCarList$.take(1)
*/
const result$ = forkJoin([
this.store.select(selectOne).pipe(take(1)),
this.store.select(selectTwo).pipe(take(1)),
this.store.select(selectThree).pipe(take(1))
@hollygood
hollygood / angular6-disable-reactive-form-field
Last active March 12, 2019 13:34
Disable Angular 6 Reactive Form Fields dynamically
// In Angular 6, if you set 'disabled' reactive form field like following,
// you will get a warning message: It looks like you're using the disabled attribute with a reactive form directive.
// If you set disabled to true when you set up this control in your component class, the disabled attribute will
// actually be set in the DOM for you. We recommend using this approach to avoid 'changed after checked' errors.
<input
formControlName="userId"
type="text"
class="form-control m-input"
required
@hollygood
hollygood / function-params-object-destructure.ts
Created January 7, 2019 14:13
Function parameters with object destructuring
// before
function renderList(list, ordered, color, bgColor) {
// set defaults for optional parameters
if (typeof ordered === undefined) ordered = true
if (typeof color === undefined) color = '#1e2a2d'
if (typeof bgColor === undefined) color = 'transparent'
/* code to render list would go here 😉 */
}
@hollygood
hollygood / pass-async-data-to-child.ts
Created January 8, 2019 14:20
Angular 6 Pass Async Data to Child Components
// blogger.component.ts
...
template: `
<h1>Posts by: {{ blogger }}</h1>
<div>
<posts [data]="posts"></posts>
</div>
`
...
@hollygood
hollygood / ngrx-effects-same-effect-for-multiple-actions.ts
Last active January 25, 2019 14:05
Example for multiple actions sharing a same effect.
@Effect({dispatch: false})
exampleFailure$: Observable<Action> = this.actions$.pipe(
ofType(SOME_ACTIONS.CREATE_USER_FAILURE, SOME_ACTIONS.CREATE_POST_FAILURE),
tap(action => {
this.router.navigate(['/users'])
})
);
@hollygood
hollygood / examples-for-ngrx-effects.ts
Last active January 25, 2019 14:31
Some examples for ngrx effects
// One input, one output
@Effect()
firstAction$: Observable<Action> = this.action$.pipe(
ofType<firstAction>('FIRST_ACTION'),
mapTo(new SecondAction())
);
// Two inputs, one output
@Effect()
public dashboardLoadRequired$: Observable<Action> =
@hollygood
hollygood / mutating-vs-non-mutating-functions.js
Created February 3, 2019 00:46
Javascript Mutating vs Non-Mutating functions
// ADD: Mutating
let mutatingAdd = ['a', 'b', 'c', 'd', 'e'];
// array.push() adds an item to the end of the array
// array.unshift() adds an item to the beginning of the array.
mutatingAdd.push('f'); // ['a', 'b', 'c', 'd', 'e', 'f']
mutatingAdd.unshift('z'); // ['z', 'a', 'b', 'c', 'd', 'e'
// ADD: NON MUTATING
// concat
@hollygood
hollygood / ngrxintro.md
Created February 7, 2019 19:05 — forked from btroncone/ngrxintro.md
A Comprehensive Introduction to @ngrx/store - Companion to Egghead.io Series

Comprehensive Introduction to @ngrx/store

By: @BTroncone

Also check out my lesson @ngrx/store in 10 minutes on egghead.io!

Update: Non-middleware examples have been updated to ngrx/store v2. More coming soon!

Table of Contents