Wassim Chegham PRO
Senior Developer Advocate @Microsoft ★ Angular contributor ★ Bazel contributor ★ GDE @Google ★ creator of @itsjustangular / hueaction.dev / ngx.tools / xlayers.dev / angular.run / thundr.dev
❤︎
by Wassim Chegham — @manekinekko
😍
Wassim Chegham
Developer Advocate
@manekinekko
I have no idea what changed, so I will just check everything that may need updating — $digest
Change And Its Detection In JavaScript Frameworks
— Tero Parviainen
ANGULARJS DIRTY CHECKING
you choose
wikipedia
in other words
CLICK
EVENT
KEY
EVENT
XHR
EVENT
ERROR
END
THIS IS A TIMELINE
THIS IS A STREAM
with
rxjs is
&
observables are
observables can
observables have operators
a simple example
import "rxjs/add/observable/range";
import "rxjs/add/operator/filter";
import "rxjs/add/operator/map";
import "rxjs/add/operator/scan";
range(0, 10)
.filter(x => x % 2 === 0)
.map(x => x + x)
.scan((acc, x) => acc + x, 0)
.subscribe(x => console.log(x));
create a stream
transform it
subscribe to it
import { range } from "rxjs/observable/range";
import { map } from "rxjs/operators/map";
import { filter } from "rxjs/operators/filter";
import { scan } from "rxjs/operators/scan";
range(0, 10)
.pipe(
filter(x => x % 2 === 0),
map(x => x + x),
scan((acc, x) => acc + x, 0)
)
.subscribe(x => console.log(x));
reminder
@Component({
selector: 'model-driven-forms',
template: `
<form (ngSubmit)="onSubmit()" [formGroup]="mdf">
<label for="email">Email</label>
<input type="email" formControlName ="email">
<label for="comments">Comments</label>
<textarea formControlName ="comments"></textarea>
</form>
`
})
class ModelDrivenForms {
mdf: FormGroup;
constructor(fb: FormBuilder) {
this.mdf = fb.group({
'email': ['', Validators.required],
'comments': ['']
});
}
}
// import {map, filter} from 'rxjs/operators';
this.mdf.valueChanges
.pipe(
map( value => {
value.email = this.sanitize(value.email);
return value;
}),
filter( value => this.form.valid)
)
.subscribe( value => print(`
Model Driven Form valid value:
vm = ${JSON.stringify(value)}
`));
// import {map, flatMap} from 'rxjs/operators';
this.mdf.ngSubmit
.pipe(
map( _ => this.mdf.controls['email'] ),
flatMap( _ => /* push to server */)
)
.subscribe(
value => console.log(value),
error => console.trace(error),
() => console.info('done')
)
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Config} from './app.config';
interface ItemsResponse { ... }
@Injectable()
class Fetch {
//...
constructor(private http: HttpClient) {
this.stream$ = this.http.get<ItemsResponse>(Config.DATA_URL);
}
//...
}
// other imports ...
import {interval} from 'rxjs/observable/interval';
import {map, flatMap, startWith, toArray} from 'rxjs/operators';
@Injectable() class Fetch {
contructor(loader: FetcherService) {
this.stream$ = interval(Config.TIMER)
.pipe(
flatMap(_ => loader.fetch()),
map((facts: any) => this.randomize(facts.data))
)
.subscribe(
data => this.randomQuote = data,
err => this.logError(err),
() => console.log('Random Quote Complete')
);
}
//...
}
import {Component} from '@angular/core';
@Component({
selector: 'parent-cmp',
template: `<child-cmp
(onChildEvent)="receiveChildEvent($event)">
</child-cmp>`
})
export class ParentComponent {
receiveChildEvent(value) {
console.log(value);
}
}
import {Component, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'child-cmp'
/* configuration */
})
export class ChildCmp {
@Output() outputEvent: EventEmitter<string>;
constructor() {
this.outputEvent = new EventEmitter<string>();
this.outputEvent.emit('foo');
}
}
@manekinekko
By Wassim Chegham
A high-level introduction to Observables and its use in the Angular world with RxJs.
Senior Developer Advocate @Microsoft ★ Angular contributor ★ Bazel contributor ★ GDE @Google ★ creator of @itsjustangular / hueaction.dev / ngx.tools / xlayers.dev / angular.run / thundr.dev