You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 4, 2017. It is now read-only.
Copy file name to clipboardExpand all lines: public/docs/ts/latest/guide/style-guide.jade
+78-62Lines changed: 78 additions & 62 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,29 @@
1
1
include../_util-fns
2
2
3
3
:marked
4
-
Welcome to the Angular Style Guide
5
-
6
-
## Purpose
7
-
8
4
Looking for an opinionated guide to Angular syntax, conventions, and application structure?
9
5
Step right in!
10
-
This style guide presents our preferred conventions and, as importantly, explains why.
6
+
This style guide presents preferred conventions and, as importantly, explains why.
7
+
11
8
.l-main-section
9
+
a(id='toc')
12
10
11
+
:marked
12
+
# Contents
13
+
14
+
* [Single responsibility](#single-responsibility)
15
+
* [Naming](#naming)
16
+
* [Coding conventions](#coding-conventions)
17
+
* [App structure and Angular modules](#application-structure-and-angular-modules)
18
+
* [Components](#components)
19
+
* [Directives](#directives)
20
+
* [Services](#services)
21
+
* [Data services](#data-services)
22
+
* [Lifecycle hooks](#lifecycle-hooks)
23
+
* [Appendix](#appendix)
24
+
25
+
26
+
.l-main-section
13
27
:marked
14
28
## Style vocabulary
15
29
@@ -32,6 +46,11 @@ include ../_util-fns
32
46
.s-rule.avoid
33
47
:marked
34
48
**Avoid** indicates something you should almost never do. Code examples to *avoid* have an unmistakeable red header.
49
+
50
+
.s-why
51
+
:marked
52
+
**Why?** gives reasons for following the previous recommendations.
53
+
35
54
.l-main-section
36
55
37
56
:marked
@@ -40,37 +59,20 @@ include ../_util-fns
40
59
Some code examples display a file that has one or more similarly named companion files.
41
60
For example, `hero.component.ts` and `hero.component.html`.
42
61
43
-
The guideline will use the shortcut `hero.component.ts|html|css|spec` to represent those various files. Using this shortcut makes this guide's file structures easier to read and more terse.
1. [App structure and Angular modules](#application-structure-and-angular-modules)
55
-
1. [Components](#components)
56
-
1. [Directives](#directives)
57
-
1. [Services](#services)
58
-
1. [Data services](#data-services)
59
-
1. [Lifecycle hooks](#lifecycle-hooks)
60
-
1. [Appendix](#appendix)
62
+
The guideline uses the shortcut `hero.component.ts|html|css|spec` to represent those various files. Using this shortcut makes this guide's file structures easier to read and more terse.
This helps make the app cleaner, easier to read and maintain, and more testable.
70
73
71
-
a#rule-of-one
72
-
:marked
73
-
### <a id="01-01"></a>_Rule of One_
74
+
75
+
### <a id="01-01"></a>Rule of One
74
76
#### <a href="#01-01">Style 01-01</a>
75
77
.s-rule.do
76
78
:marked
@@ -95,7 +97,9 @@ a#rule-of-one
95
97
:marked
96
98
The key is to make the code more reusable, easier to read, and less mistake prone.
97
99
98
-
The following *negative* example defines the `AppComponent`, bootstraps the app, defines the `Hero` model object, and loads heroes from the server ... all in the same file. *Don't do this*.
100
+
The following *negative* example defines the `AppComponent`, bootstraps the app,
101
+
defines the `Hero` model object, and loads heroes from the server all in the same file.
**Do** structure the app such that you can `L`ocate code quickly,
976
-
`I`dentify the code at a glance,
977
-
keep the `F`lattest structure you can, and
978
-
`T`ry to be DRY.
979
+
**Do** structure the app such that you can **L**ocate code quickly,
980
+
**I**dentify the code at a glance,
981
+
keep the **F**lattest structure you can, and
982
+
**T**ry to be DRY.
979
983
980
984
.s-rule.do
981
985
:marked
@@ -1000,8 +1004,7 @@ a(href="#toc") Back to top
1000
1004
1001
1005
.s-why.s-why-last
1002
1006
:marked
1003
-
**Why?**
1004
-
To work efficiently you must be able to find files quickly,
1007
+
**Why?** To work efficiently you must be able to find files quickly,
1005
1008
especially when you do not know (or do not remember) the file _names_.
1006
1009
Keeping related files near each other in an intuitive location saves time.
1007
1010
A descriptive folder structure makes a world of difference to you and the people who come after you.
@@ -1077,7 +1080,7 @@ a(href="#toc") Back to top
1077
1080
1078
1081
.s-rule.do
1079
1082
:marked
1080
-
**Do** be DRY (Don't Repeat Yourself)
1083
+
**Do** be DRY (Don't Repeat Yourself).
1081
1084
1082
1085
.s-rule.avoid
1083
1086
:marked
@@ -1087,7 +1090,8 @@ a(href="#toc") Back to top
1087
1090
:marked
1088
1091
**Why?** Being DRY is important, but not crucial if it sacrifices the other elements of LIFT.
1089
1092
That's why it's called _T-DRY_.
1090
-
For example, it's redundant to name a component, `hero-view.component.html` because a component is obviously a view.
1093
+
For example, it's redundant to name a template `hero-view.component.html` because
1094
+
with the `.html` extension, it is obviously a view.
1091
1095
But if something is not obvious or departs from a convention, then spell it out.
1092
1096
1093
1097
a(href="#toc") Back to top
@@ -1203,20 +1207,23 @@ a(href="#toc") Back to top
1203
1207
1204
1208
.s-why
1205
1209
:marked
1206
-
**Why?** A developer can locate the code, identify what each file represents
1207
-
at a glance, the structure is as flat as it can be, and there are no repetitive or redundant names.
1210
+
**Why?** A developer can locate the code and identify what each file represents
1211
+
at a glance. The structure is as flat as it can be and there are no repetitive or redundant names.
1208
1212
1209
1213
.s-why
1210
1214
:marked
1211
1215
**Why?** The LIFT guidelines are all covered.
1212
1216
1213
1217
.s-why
1214
1218
:marked
1215
-
**Why?** Helps reduce the app from becoming cluttered through organizing the content and keeping them aligned with the LIFT guidelines.
1219
+
**Why?** Helps reduce the app from becoming cluttered through organizing the
1220
+
content and keeping them aligned with the LIFT guidelines.
1216
1221
1217
1222
.s-why
1218
1223
:marked
1219
-
**Why?** When there are a lot of files (e.g. 10+), locating them is easier with a consistent folder structure and more difficult in a flat structure.
1224
+
**Why?** When there are a lot of files, for example 10+,
1225
+
locating them is easier with a consistent folder structure
1226
+
and more difficult in a flat structure.
1220
1227
1221
1228
.s-rule.do
1222
1229
:marked
@@ -1243,7 +1250,8 @@ a(href="#toc") Back to top
1243
1250
1244
1251
.s-rule.do
1245
1252
:marked
1246
-
**Do** create an Angular module in the app's root folder (e.g., in `/src/app`).
1253
+
**Do** create an Angular module in the app's root folder,
1254
+
for example, in `/src/app`.
1247
1255
1248
1256
.s-why
1249
1257
:marked
@@ -1269,19 +1277,23 @@ a(href="#toc") Back to top
1269
1277
.s-rule.do
1270
1278
1271
1279
:marked
1272
-
**Do** create an Angular module for all distinct features in an application (e.g. `Heroes` feature).
1280
+
**Do** create an Angular module for all distinct features in an application;
1281
+
for example, a `Heroes` feature.
1273
1282
1274
1283
.s-rule.do
1275
1284
:marked
1276
-
**Do** place the feature module in the same named folder as the feature area (.e.g `app/heroes`).
1285
+
**Do** place the feature module in the same named folder as the feature area;
1286
+
for example, in `app/heroes`.
1277
1287
1278
1288
.s-rule.do
1279
1289
:marked
1280
-
**Do** name the feature module file reflecting the name of the feature area and folder (e.g. `app/heroes/heroes.module.ts`)
1290
+
**Do** name the feature module file reflecting the name of the feature area
1291
+
and folder; for example, `app/heroes/heroes.module.ts`.
1281
1292
1282
1293
.s-rule.do
1283
1294
:marked
1284
-
**Do** name the feature module symbol reflecting the name of the feature area, folder, and file (e.g. `app/heroes/heroes.module.ts` defines `HeroesModule`)
1295
+
**Do** name the feature module symbol reflecting the name of the feature
1296
+
area, folder, and file; for example, `app/heroes/heroes.module.ts` defines `HeroesModule`.
1285
1297
1286
1298
.s-why
1287
1299
:marked
@@ -1316,7 +1328,8 @@ a(href="#toc") Back to top
1316
1328
1317
1329
.s-rule.do
1318
1330
:marked
1319
-
**Do** create a feature module named `SharedModule` in a `shared` folder (e.g. `app/shared/shared.module.ts` defines `SharedModule`).
1331
+
**Do** create a feature module named `SharedModule` in a `shared` folder;
1332
+
for example, `app/shared/shared.module.ts` defines `SharedModule`.
1320
1333
1321
1334
.s-rule.do
1322
1335
:marked
@@ -1325,7 +1338,7 @@ a(href="#toc") Back to top
1325
1338
1326
1339
.s-rule.consider
1327
1340
:marked
1328
-
**Consider** using the name SharedModule, when the contents of a shared
1341
+
**Consider** using the name SharedModule when the contents of a shared
1329
1342
module are referenced across the entire application.
1330
1343
1331
1344
.s-rule.do
@@ -1336,11 +1349,14 @@ a(href="#toc") Back to top
1336
1349
1337
1350
.s-rule.do
1338
1351
:marked
1339
-
**Do** import all modules required by the assets in the `SharedModule` (e.g. `CommonModule` and `FormsModule`).
1352
+
**Do** import all modules required by the assets in the `SharedModule`;
1353
+
for example, `CommonModule` and `FormsModule`.
1340
1354
1341
1355
.s-why
1342
1356
:marked
1343
-
**Why?** `SharedModule` will contain components, directives and pipes that may need features from another common module (e.g. `ngFor` in `CommonModule`).
1357
+
**Why?** `SharedModule` will contain components, directives and pipes
1358
+
that may need features from another common module; for example,
1359
+
`ngFor` in `CommonModule`.
1344
1360
1345
1361
.s-rule.do
1346
1362
:marked
@@ -1642,7 +1658,7 @@ a(href="#toc") Back to top
1642
1658
:marked
1643
1659
**Why?** components have templates containing HTML and optional Angular template syntax.
1644
1660
They display content.
1645
-
Developers place components on the page as they would native HTML elements and WebComponents.
1661
+
Developers place components on the page as they would native HTML elements and web components.
1646
1662
1647
1663
.s-why.s-why-last
1648
1664
:marked
@@ -1726,7 +1742,7 @@ a(href="#toc") Back to top
1726
1742
1727
1743
.s-rule.do
1728
1744
:marked
1729
-
**Do** use the `@Input` and `@Output` class decorators instead of the `inputs` and `outputs` properties of the
1745
+
**Do** use the `@Input()` and `@Output()` class decorators instead of the `inputs` and `outputs` properties of the
1730
1746
`@Directive` and `@Component` metadata:
1731
1747
1732
1748
.s-rule.do
@@ -1937,7 +1953,7 @@ a(href="#toc") Back to top
1937
1953
1938
1954
.s-why
1939
1955
:marked
1940
-
**Why?** Attributes directives don't have an associated template.
1956
+
**Why?** Attribute directives don't have an associated template.
1941
1957
1942
1958
.s-why.s-why-last
1943
1959
:marked
@@ -1968,7 +1984,7 @@ a(href="#toc") Back to top
1968
1984
.s-why.s-why-last
1969
1985
:marked
1970
1986
**Why?** The property associated with `@HostBinding` or the method associated with `@HostListener`
1971
-
can be modified only in a single place - in the directive's class.
1987
+
can be modified only in a single place—in the directive's class.
1972
1988
If you use the `host` metadata property, you must modify both the property declaration inside the controller,
1973
1989
and the metadata associated with the directive.
1974
1990
@@ -2075,7 +2091,7 @@ a(href="#toc") Back to top
2075
2091
2076
2092
.s-rule.do
2077
2093
:marked
2078
-
**Do** use the `@Injectable` class decorator instead of the `@Inject` parameter decorator when using types as tokens for the dependencies of a service.
2094
+
**Do** use the `@Injectable()` class decorator instead of the `@Inject` parameter decorator when using types as tokens for the dependencies of a service.
0 commit comments