Interval and Clock Ticker
Use IntervalObservable in rxjs
to implement interval in angular 2. Create(period)
is a static method, which creates an Observable that emits sequential numbers every specified interval of time.
import { IntervalObservable } from 'rxjs/observable/IntervalObservable'; |
ngOnChanges
Not Detect Changes in Object
During change detection, when Angular checks components’ input properties for change, it uses (essentially) === for dirty checking. For arrays, this means the array references (only) are dirty checked. Since the rawLapsData
array reference isn’t changing, ngOnChanges()
will not be fired.
Two possible solutions:
- Implement
ngDoCheck()
and perform your own change detection logic to determine if the array contents have changed. - Assign a new array to
rawLapsData
whenever you make any changes to the array contents. ThenngOnChanges()
will be called because the array (reference) will appear as a change.
Number Pipe Parameters
For example,{{ tmpNumber | number : '1.2-2' }}
The parameter has this syntax:
{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
So the example of ‘1.2-2’ means:
- A minimum of 1 digit will be shown before decimal point.
- Show at least 2 digits after decimal point.
- No more than 2 digits.
Matrix URL
Matrix parameters are tied to a path segment, while query parameters are tied to the URL. They have different semantics.
Maybe it’s hard to tell because you always see it at the end of the URL, but this is also matrix paramterslocalhost:3000/heroes;id=15;foo=foo/bar/baz
The parameters are tied to heroes. When you access the route.url, you will see this
this.route.url.subscribe((url: UrlSegment[]) => { |
Is the matrix url notation the “default” to creating urls with parameters or is better to use the “old” notation with ?
No, both could be used, and how to use (create) them is completely different
matrix parameters are tied to each path, by passing an object after the path in the array
router.navigate(['/foo', { id:1 }, 'bar', {baz: 2 } ])
Here you will get /foo;id=1/bar;baz=2
.
Query parameters are created by passing the NavigationExtras as the second argument to navigate
router.navigate(['/foo'], { queryParams: { bar: 1, baz: 2 }});
Here you get /foo?bar=1&baz=2
See also:
When to use query parameters versus matrix parameters?
URL matrix parameters vs. request parameters
canActivate
Return Observable
Note:
CanActive
is an interface, that indicates a class can be implement to be a guard deciding if a route can be activated.- Implement
canActivate()
method. It can return with 3 types.canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<boolean>|Promise<boolean>|boolean
- We can get previous path through RouterStateSnapshot.
- Passing parameters via router.navigate(), and the passed params are matrix URL pattern rather than query params.
Have to pass the matrix URL parameters to the redirected error page, so that the error page knows what is the link that user wants to access causing an error. - Hope to replace
window.location.href
in the future, it is not written in angular way.
canActivate(route:ActivatedRouteSnapshot, state:RouterStateSnapshot): Observable<boolean> { |
Detect Route Changes
import { Router, NavigationStart, NavigationEnd, NavigationError, NavigationCancel, RoutesRecognized } from '@angular/router'; |
You can also filter by the given event:
import 'rxjs/add/operator/filter'; |
Using pairwise operator to get the previous and current event also is an nice idea.
import 'rxjs/add/operator/pairwise'; |
Ref: