Posted onEdited onInjavascript Symbols count in article: 372Reading time ≈1 mins.
Introduction to javascript operator ||
In JavaScript, the || operator is used to return the first truthy value among its operands. If all operands are falsy values, it returns the last operand.
1 2 3 4 5 6
x || y // return x if x is truthy, otherwise return y x || y || z // return the first truthy value among x, y, z, or z if all operands are falsy values. // examples 1 || 2// 1 0 || 1// 1 0 || "" || 1// 1
|| can return non-boolean values
Logical operator or || in most programming languages only return boolean values, but in JavaScript, this is not true.
1 2 3 4
1 || 2// 1, number type "hello" || "world"// "hello", string type 0 || null// null, null type 0 || undefined// undefined, undefined type
arguments checking
|| is often used to check if an argument is provided or not. If the argument is not provided, a default value will be used.
In above example, if timeout or callback is not provided, the default value will be used.
But there is a problem with this approach, if the argument is provided but falsy value(for example, pass 0 for timeout), the default value will be used. In the following code, we intend to use 0 as the timeout value, but 0 || 2000 = 2000, so 2000 was used as the timeout value.
Posted onEdited onInnx Symbols count in article: 125Reading time ≈1 mins.
Generate project with default styling file extension
Nx based monorepo can use file nx.json to config project generate options for specific frameworks. Take the following config as an example, when creating angular applications, nx will use scss as file extension for style files by default. If you want the terminal prompt you during the generation, remove the style option from the config.
Posted onEdited onInangular Symbols count in article: 126Reading time ≈1 mins.
The built-in pipe json is used to convert a JavaScript object into a JSON string. It is useful for debugging purposes or when you need to display the raw JSON data in your Angular application.
Usage
Assume you have a data object in your components which is an javascript object:
1 2 3 4 5 6
{ "userId": 1, "id": 20, "title": "ullam nobis libero sapiente ad optio sint", "completed": true }
In your template file, you can use the json pipe to convert the object into a JSON string:
1
<pre>{{ data | json }}</pre>
You will get the following output on the rendered page:
1
{ "userId": 1, "id": 20, "title": "ullam nobis libero sapiente ad optio sint", "completed": true }
Without the json pipe, you will get [object Object] in the output.
Posted onEdited onInangular Symbols count in article: 908Reading time ≈3 mins.
Introduction
Deferrable views is a new feature in Angular 18 that allows you to defer the loading of a component until it is needed. This can help improve the performance of your application by reducing the initial bundle size.
How to use deferrable views
Load when browser is idle
Wrap your component with @defer {} to make it deferrable. In the following example, the app-product-detail component will be loaded only when the Browser is idle.
You can also specify a condition for when the component should be loaded. In the following example, the app-product-detail component will be loaded when it enters the viewport.
Placeholder
Note that, defer on viewport must be used in conjunction with @placeholder directive. When component is out of viewport, the placeholder will be rendered.
Minium render time for placeholder and loading section.
To avoid flickering, you can specify a minimum render time for the placeholder and loading section. In the following example, the placeholder will be rendered for at least 200ms before the component is loaded, and the loading section will be rendered for at least 500ms.
Note that at least doesn’t mean exactly, for example, if the component didn’t load in 500ms, the loading section will keep showing until the component is loaded.
The component will be loaded when the specified content enters the viewport. If you didn’t specify the content, the component will be loaded when itself enters the viewport.
You can also load the component when a specific element enters the viewport. In the following example, the app-product-detail component will be loaded when the greeting element enters the viewport.
1 2 3 4
<div #greeting>Hello!</div> @defer (on viewport(greeting)) { <app-product-detail></app-product-detail> }
interaction
The interaction trigger loads the deferred content when user interacts withe the specific element through click or keydown event.
By default, the placeholder acts as the interaction element. Placeholders used this way must have a single root element.
You can also specify a different element for the interaction trigger. In the following example, the app-product-detail component will be loaded when the greeting element is clicked.
1 2 3 4
<div #greeting>Hello!</div> @defer (on interaction(greeting)) { <app-product-detail></app-product-detail> }
hover
Same as interaction, you can just replace interaction with hover in the above examples.
immediate
This is different than idle, the component will be loaded immediately after the non-deferred content has finished rendering.
The when trigger accepts a custom conditional expression and loads the deferred content when the condition becomes true. You can control the condition in our component class.
This is a one-time operation– the @defer block does not revert back to the placeholder if the condition changes to a falsy value after becoming truthy.
Conclusion
Conditions for deferrable views
Must be standalone.
Must not references outside of the @defer block in the same template file.
How it works
Angular use dynamic import to load the deferrable views.
The component loaded in @defer was bundled in a separated chunk.
Posted onEdited onInangular Symbols count in article: 382Reading time ≈1 mins.
Introduction
Angular has a built-in pipe async that can be used to subscribe to an Observable or Promise and return the latest value it has emitted.
Without async pipe
Let’s see how we write the code without using the async pipe. In the following OrderComponent, we have a todo object that is fetched from the DataService using the fetchTodoById method which returns an Promise. We call the fetchTodos method in the ngOnInit lifecycle hook and assign the result to the todo property.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
exportclassOrderComponentimplementsOnInit { todo: Todo | null = null;
With async pipe, we don’t need to call then method on Promise, the async pipe will take care of it. We can directly assign the result of fetchTodos method to the todo property and use the async pipe in the template to subscribe to the Promise.
We need two changes in the code:
Change the type of todo property to Promise<Todo> | null.
Assign the result of fetchTodos method to the todo property.
If using with Observable, just change the type of todo property to Observable<Todo> | null.
1
todo: Observable<Todo> | null = null; // If using Observable
Using with ngFor
1 2 3 4 5 6
<div *ngFor="let todo of todos | async"> <div>id: {{todo.id}}</div> <div>userId: {{todo.userId}}</div> <div>title: {{todo.title}}</div> <div>completed: {{todo.completed}}</div> </div>
Benefits
When use with Observables, the async pipe automatically subscribes to the Observable, renders the output within the template, and then unsubscribes when the component is destroyed. - You don’t need to manually subscribe and unsubscribe.