Angular Recipes
Some tips and tricks for specific cases in Angular
Tips & tricks for tricky but useful things in angular 2. The type of problems that are too small to deserve a full blog post, and that you do not know how to name to search for.
Elvis operator and bracket notation
Bracket notation is javascript is an alternative to dot notation to display the content of an object. The main advantage is has is that you can use variable inside the bracket to display a property whose name depends of a variable.
Elvis operator is a syntax for angular 2 that allow telling angular renderer to ignore template binding of sub-properties for object that does not exist yet. Indeed, it is generally that the object is not yet loaded from the backend.
From this link
The Elvis operator is only available for the . notation, not for other operators like []. As a workaround use
Select an element of the DOM
Well explained here
Use ViewChild with #localvariable as shown here,
In component,
New API with renderer....
Prevent error when module are failing to be imported
Module handling in JS is a headache. Sometimes it just don't works. But you still need to use a js lib the old way (with a script in the index.html).
You can fool typescript to allow you to use the missing library (that will exist at run time).
Object property binding & async pipe with bracket syntax
Yes but how to use a bracket syntax with async syntax.
I have an observable that I want to pass to a component. Once passed I want a sub property that is static in that case : the first item of an array. Or may be dynamic, a component property.
The problem is that : (imagesFB | async)[0]
would fail because the item does not exist at first and the async is for imagesFB not for the resulting array that (imagesFB | async) return.
So with a ternary syntax :
We do the trick.
Instantiating a class or static method
There are 3 keywords that can be used in a class. To defined a property or method
public to get the method available from other classes
private to get it only usable inside the class
static allow to use a class without instantiating it
Instantiate a class, is creating a local copy of it. For example if we are in a component, that mean that we are using an instance of a class.
An instance is creating by passing it to the component constructor like this.
But by using static we do not need, and we cannot actually create a local instance
Simplify module definition with the spread operator
Export of modules to be compatible with lazy loading, AOT and universal angular
Navigate to a defined page in the component or service
Do somethings after 2 or more promises resolve
Usage of Rxjs Observable.combineLatest to do something and combine the results of the 2 or more promises resolved.
Redirect to the same path but changing one query params
The trick is [] to tell same page.
Redirect to the same path changing only the last path parameter
The trick is to use relative path syntax of angular 2 router
watching queryparams and params changes
queryparams are optional
?step=2&lang=fr
so step and langparams are in the path
/project/:projectId
so project Id in that cas
For both we use the Activated route service, so we place in our constructor
then we set a watcher in our ngInit()
For query params read this good tuto for more info
Execute a js library outside of the scope of angular
Angular run inside an execution context so is not accessible in the global scope of the page. But some libraries like jQuery exist in the global scope of the page.
So here is an example on how to run a jquery library from an angular component.
Import of the NgZone module
Definition in the component constructor
Use of this.zone.runOutsideAngular(() => {
to switch execution zone
Add even, odd and last class to ngFor
will result in
Dynamic templates
In angular 2 your can use dynamic templates called by name Primeng library make heavy use of it, you can see as an illustration templates definitions here
Injection parent component in child component
Example and explanations in the slick-carousel integration tutorial
@ContentChildren() allow to have a reference to a child component in the parent component
@Host() allow to have a reference to a parent component in the child component
Http interceptor
Http interceptor intercept http requests for example to add a token to all API request.
Define interceptor service in the app.module.ts
Example of interceptor file
Execute a function with a delay in an angular component
Use environment variable in your component or service
https://medium.com/beautiful-angular/angular-2-and-environment-variables-59c57ba643be
Async handling automation and Progressive Web app
https://medium.com/@cyrilletuzi/angular-async-pipe-everywhere-with-ngx-pwa-offline-d8de68845c81
Sharing data / method between component
https://angularfirebase.com/lessons/sharing-data-between-angular-components-four-methods/
Setup of universal server side rendering (updated)
https://angularfirebase.com/lessons/server-side-rendering-firebase-angular-universal/`
Angular Forms : setValue vs patchValue
https://ultimatecourses.com/blog/angular-2-form-controls-patch-value-set-value
Return a mock if server is off
Multiple service calls
Router lazy load nested path
Last updated