1. 使用 ng-container 来包裹多个元素,而不需要额外的 div 或者 span 等元素。
1 2 3 4 5
<!-- This way will generate an extra div element --> <div *ngIf="true"> <div>Content 1</div> <div>Content 2</div> </div>
1 2 3 4 5
<!-- This way will not generate any extra div element --> <ng-container *ngIf="true"> <div>Content 1</div> <div>Content 2</div> </ng-container>
2. 使用 ng-container来分隔多个结构化指令。
在Angular中,一个元素只能有一个结构性指令,比如 *ngIf, *ngFor 等等。如果需要在一个元素上使用多个结构性指令,可以使用 ng-container 来分隔。 比如下面的代码我们在li元素上同时使用了ngFor和ngIf,这会导致编译错误:Only one structural directive can be applied
// Custom Theming for Angular Material // For more information: https://material.angular.io/guide/theming @use'@angular/material' as mat; // Plus imports for other components in your app.
// Include the common styles for Angular Material. We include this here so that you only // have to load a single css file for Angular Material in your app. // Be sure that you only ever include this mixin once! @include mat.core();
// Define the palettes for your theme using the Material Design palettes available in palette.scss // (imported above). For each palette, you can optionally specify a default, lighter, and darker // hue. Available color palettes: https://material.io/design/color/ $my-app-primary: mat.define-palette(mat.$teal-palette); $my-app-accent: mat.define-palette(mat.$teal-palette, A200, A100, A400);
// The warn palette is optional (defaults to red). $my-app-warn: mat.define-palette(mat.$red-palette);
// Create the theme object. A theme consists of configurations for individual // theming systems such as "color" or "typography". $my-app-theme: mat.define-light-theme(( color: ( primary: $my-app-primary, accent: $my-app-accent, warn: $my-app-warn, ), typography: mat.define-typography-config(), density: 0 ));
// Include theme styles for core and each component used in your app. // Alternatively, you can import and @include the theme mixins for each component // that you are using. @include mat.all-component-themes($my-app-theme);
Put the theme file in angular.json or project.json(for Nx monorepo), Architect -> build -> options -> styles
Posted onEdited onInangular Symbols count in article: 227Reading time ≈1 mins.
The @Output decorator is used to define an output property in a component. The output property is used to emit events from the component to the parent component. The @Output decorator is used in conjunction with the EventEmitter class to emit events.
The connection between child component and parent component is established by binding the updateParentName output property of the child component to the updateName method of the parent component.
Posted onEdited onInangular Symbols count in article: 465Reading time ≈2 mins.
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.
From Angular 17, you can use the first format with some extra configuration in ChildComponent. In this way Angular will automatically convert the flag attribute to a boolean value.
You can use the following cli command to transit your app to standalone.
1
ng generate @angular/core:standalone
and you’ll see the following options in terminal, use the arrow key to select the options.
1 2 3 4
? Choose the type of migration: (Use arrow keys) > Convert all components, directives and pipes to standalone Remove unnecessary NgModule classes Bootstrap the application using standalone APIs