Posted onEdited onInrxjs Symbols count in article: 303Reading time ≈1 mins.
什么是背压
Back pressure 翻译成中文叫做背压,也有叫回压的,那么到底啥是背压呢?望文思义一下,背压就是来自背后的压力,那么背后是指哪里呢?在RxJS中,背后就是指产生数据的Observable,我们姑且称之为生产者,而订阅这个Observable的Observer就是消费者。背压就是生产者生产数据的速度大于消费者消费的速度,导致大量的数据积压。
<!--app.component.html--> <divclass="account-feature-container" > <ng-container [ngSwitch]="accountStatus"> <ng-container *ngSwitchCase="AccountStatus.CLOSED"> <p>This is a closed account</p> </ng-container> <ng-container *ngSwitchCase="AccountStatus.AUTHORIZED"> <p>This is an authorized account</p> </ng-container> <ng-container *ngSwitchCase="AccountStatus.NORMAL"> <p>This is an normal account</p> </ng-container> <p *ngSwitchDefault>This is the default branch</p> </ng-container> </div>
<divclass="use_built-in_control_flow"> @switch (accountStatus) { @case (AccountStatus.CLOSED) { <p>This is a closed account</p> } @case (AccountStatus.AUTHORIZED) { <p>This is an authorized account</p> } @case (AccountStatus.NORMAL) { <p>This is an normal account</p> } @default { <p>This is the default branch</p> } } </div>
Tips:
Remember to add the @default branch at the end, if no case matched, and no @default is provided, nothing will be rendered.
There is no fallthrough in the @switch directive, so you don’t need to add break or return in each @case branch.
Posted onEdited onInangular Symbols count in article: 476Reading time ≈2 mins.
Introduction
ngIf is a structural directive in Angular, it is used to conditionally render content in a template. Remember to import CommonModule in the module where you want to use ngIf.
Full syntax with ng-template
Structural directives are used in ng-template, here is an example:
But, in practice, we often use ngIf in a more concise way by prefix a * before it, this is called the shorthand syntax:
1
<div *ngIf="hero"class="name">{{hero.name}}</div>
Note, even we write it in the shorthand syntax, Angular will still convert it to the ng-template syntax after compiling.
The above template will create a div element in the template, What if you don’t want to create a div element? You can use ng-container instead of div:
1 2 3
<ng-container *ngIf="condition"> Content to render when condition is true </ng-container>
In this way, Angular will render the text directly without creating a div element.
New syntax with @if
With the new built-in structural @if directive, you can use the following syntax:
1 2 3
@if (condition) { Content to render when condition is true }
If else then with *ngIf
1 2 3 4 5 6 7 8 9 10
<divclass="case-3"> <ng-container *ngIf="condition; then thenBlock else elseBlock"> </ng-container> <ng-template #thenBlock> Content to render when condition is true </ng-template> <ng-template #elseBlock> Content to render when condition is false </ng-template> </div>
If else then with @if
With @if directive, you can use the following syntax, it’s more simple and readable:
1 2 3 4 5 6 7
<divclass="case-3"> @if (condition) { Content to render when condition is true } else { Content to render when condition is false } </div>
Reducing initial bundle size, Zone.js is about 30KB raw and around 10KB gzipped. Remove it can significantly save the initial load time.
Avoid unnecessary change detection cycles: Zone.js notify Angular to run change detection on every async operation, but it doesn’t actually know whether these operations change any data.
Improve debugging experience, Zone.js can make stack traces harder to read.
Integration steps
Create an Angular 18 project
1
npx @angular/cli@18 new my-zoneless-app
Enable zoneless change detection in standalone bootstrap app.