Skip to content

Commit d797c5c

Browse files
JeanMechekirjs
authored andcommitted
docs: update class & style binding recommendation (#59240)
This commit introduces an update to the official recommendations when it comes to class & styles bindings. `[class]` & `[style]` bindings are now recommended for basic uses cases. `[ngClass]` and `[ngStyle]` allow more advanced bindings (like space separated keys) or keys with units (for `ngStyle`) which are not supported by the native bindings. They still require the dedicated directives. PR Close #59240
1 parent 2a92dcf commit d797c5c

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

‎adev/src/content/guide/directives/overview.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ These steps are not necessary to implement `ngClass`.
6969

7070
## Setting inline styles with `NgStyle`
7171

72+
HELPFUL: To add or remove a _single_ style, use [style bindings](guide/templates/binding#css-class-and-style-property-bindings) rather than `NgStyle`.
73+
7274
### Import `NgStyle` in the component
7375

7476
To use `NgStyle`, add it to the component's `imports` list.

‎packages/common/src/directives/ng_class.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import {
1010
DoCheck,
1111
ElementRef,
1212
Input,
13-
IterableDiffers,
14-
KeyValueDiffers,
1513
Renderer2,
1614
ɵstringify as stringify,
1715
} from '@angular/core';
@@ -43,17 +41,23 @@ interface CssClassState {
4341
*
4442
* @usageNotes
4543
* ```html
46-
* <some-element [ngClass]="'first second'">...</some-element>
44+
* <some-element [ngClass]="stringExp|arrayExp|objExp|Set">...</some-element>
4745
*
48-
* <some-element [ngClass]="['first', 'second']">...</some-element>
46+
* <some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>
47+
* ```
48+
*
49+
* For more simple use cases you can use the [class bindings](/guide/templates/binding#css-class-and-style-property-bindings) directly.
50+
* It doesn't require importing a directive.
4951
*
50-
* <some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>
52+
* ```html
53+
* <some-element [class]="'first second'">...</some-element>
5154
*
52-
* <some-element [ngClass]="stringExp|arrayExp|objExp">...</some-element>
55+
* <some-element [class.expanded]="isExpanded">...</some-element>
5356
*
54-
* <some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>
55-
* ```
57+
* <some-element [class]="['first', 'second']">...</some-element>
5658
*
59+
* <some-element [class]="{'first': true, 'second': true, 'third': false}">...</some-element>
60+
* ```
5761
* @description
5862
*
5963
* Adds and removes CSS classes on an HTML element.
@@ -64,6 +68,9 @@ interface CssClassState {
6468
* - `Object` - keys are CSS classes that get added when the expression given in the value
6569
* evaluates to a truthy value, otherwise they are removed.
6670
*
71+
*
72+
* @see [Class bindings](/guide/templates/binding#css-class-and-style-property-bindings)
73+
*
6774
* @publicApi
6875
*/
6976
@Directive({

‎packages/common/src/directives/ng_style.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ import {
2222
*
2323
* @usageNotes
2424
*
25-
* Set the font of the containing element to the result of an expression.
26-
*
27-
* ```html
28-
* <some-element [ngStyle]="{'font-style': styleExp}">...</some-element>
29-
* ```
30-
*
3125
* Set the width of the containing element to a pixel value returned by an expression.
3226
*
3327
* ```html
@@ -40,6 +34,15 @@ import {
4034
* <some-element [ngStyle]="objExp">...</some-element>
4135
* ```
4236
*
37+
* For more simple use cases you can use the [style bindings](/guide/templates/binding#css-class-and-style-property-bindings) directly.
38+
* It doesn't require importing a directive.
39+
*
40+
* Set the font of the containing element to the result of an expression.
41+
*
42+
* ```html
43+
* <some-element [style]="{'font-style': styleExp}">...</some-element>
44+
* ```
45+
*
4346
* @description
4447
*
4548
* An attribute directive that updates styles for the containing HTML element.
@@ -51,6 +54,8 @@ import {
5154
* is assigned to the given style property.
5255
* If the result of evaluation is null, the corresponding style is removed.
5356
*
57+
* @see [Style bindings](/guide/templates/binding#css-class-and-style-property-bindings)
58+
*
5459
* @publicApi
5560
*/
5661
@Directive({

0 commit comments

Comments
 (0)