Over the past three releases, Angular team introduced a lot of new features and improvements, Angular 18 focused on polishing the previous work and graduating many of the new APIs from experimental to stable. another exciting feature is zoneless change detection
- still in experimental phase. Here is the content table:
- Zoneless Change Detection
- angular.dev is the new home for angular developers
- Stable features: Material 3, deferrable views, built-in control flows
- SSR improvements: i18n hydration support, better debugging, hydration support in Angular Material.
- Native await for zoneless apps.
- Fallback content for
ng-content
.
Zoneless Change Detection
1 | bootstrapApplication(App, { |
Then remove zone.js from file angular.json
in your project.
Testing
HttpClientTestingModule
is deprecated, use `` instead.
Before:
1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
Now
1 | import { provideHttpClientTesting } from '@angular/common/http/testing'; |
Native await for zoneless apps
因为Angular一直使用zone.js实现更新检测,但是async/await语法zone.js一直无法做monkey patch,导致在zone.js中使用promise来模拟,现在如果你的app不是用zone.js,那么Angular就不会再使用promise来模拟async/await,而是直接使用原生的async/await。
Specifying a fallback content for ng-content
ng-content
现在可以指定一个fallback content,当没有传递内容时,会显示这个fallback content。
1 | ({ |
调用代码:
1 | <app-profile> |
which will result in:
1 | <span class="greeting">Good morning </span> |
- We specified
Good morning
for the firstng-content
, so the fallbackHello
won’t be displayed. - We didn’t specify any content for the second
ng-content
, so the fallbackUnknown user
will be displayed.
Unified control state change events
1 | const nameControl = new FormControl<string|null>('name', Validators.required); |
Router redirect as function
The redirectTo
property in the route configuration can now be a function that returns a URL string or an array of URL segments.
1 | const routes: Routes = [ |