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.
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 lang
params 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.
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
import { UploadService } from '../../../services/api/upload.service'
export class UiUploadUploadcareComponent {
constructor(
private uploadService: UploadService,
){
}
upload() {
// we use the instance of the class with this
this.uploadService.upload()
}
import { StaticService } from '../../../services/static.service'
export class UiUploadUploadcareComponent {
constructor(
// we do not need to instanciate the class
){
}
upload() {
// we use the static method useStaticClass
// of StaticService class without the this keyword
StaticService.useStaticClass()
}
// change orderId only while we are at order/22 for example
// but keeping all parameters
this.router.navigate(['../', this.order._id], { queryParams: this.params, relativeTo: this.route });
// For this path (defined in routing configuration)
{ path: 'order/:orderId', component: SROrderPage },
div(
*ngFor="let item of item; let last = last; let odd = odd; let even = even; let index = index;",
[ngClass]="{'odd': odd,'even': even,'last': last}",
class="item item-{{ index }}")
<div class="item odd item-1">
First item
</div>
<div class="item even item-2">
Second item
</div>
<div class="item odd last item-2">
Third item
</div>