2016-04-01 20:23:34 -04:00
include ../_util-fns
:marked
2016-09-19 23:24:40 -04:00
Welcome to the Angular Style Guide
2016-04-25 12:24:13 -04:00
2016-04-01 20:23:34 -04:00
## Purpose
2016-09-27 03:58:10 -04:00
Looking for an opinionated guide to Angular syntax, conventions, and application structure?
Step right in!
This style guide presents our preferred conventions and, as importantly, explains why.
2016-04-01 20:23:34 -04:00
.l-main-section
2016-04-28 03:58:50 -04:00
:marked
## Style Vocabulary
Each guideline describes either a good or bad practice, and all have a consistent presentation.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
The wording of each guideline indicates how strong the recommendation is.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** is one that should always be followed.
_Always_ might be a bit too strong of a word.
Guidelines that literally should always be followed are extremely rare.
2016-09-27 03:58:10 -04:00
On the other hand, you need a really unusual case for breaking a *Do* guideline.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.consider
:marked
2016-05-02 12:58:30 -04:00
**Consider** guidelines should generally be followed.
2016-04-28 03:58:50 -04:00
If you fully understand the meaning behind the guideline and have a good reason to deviate, then do so. Please strive to be consistent.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.avoid
:marked
2016-09-27 03:58:10 -04:00
**Avoid** indicates something you should almost never do. Code examples to *avoid* have an unmistakeable red header.
2016-04-28 03:58:50 -04:00
.l-main-section
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
:marked
## File Structure Conventions
2016-05-02 12:58:30 -04:00
Some code examples display a file that has one or more similarly named companion files. (e.g. hero.component.ts and hero.component.html).
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.
2016-04-28 03:58:50 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
a(id='toc')
2016-04-25 12:24:13 -04:00
2016-04-01 20:23:34 -04:00
:marked
## Table of Contents
1. [Single Responsibility](#single-responsibility)
2016-04-25 12:24:13 -04:00
1. [Naming](#naming)
2016-05-01 02:11:33 -04:00
1. [Coding Conventions](#coding-conventions)
2016-09-27 03:58:10 -04:00
1. [App Structure and Angular Modules](#app-structure-and-angular-modules)
2016-04-01 20:23:34 -04:00
1. [Components](#components)
2016-04-13 17:39:12 -04:00
1. [Directives](#directives)
2016-04-01 20:23:34 -04:00
1. [Services](#services)
1. [Data Services](#data-services)
2016-05-01 02:11:33 -04:00
1. [Lifecycle Hooks](#lifecycle-hooks)
2016-04-01 20:23:34 -04:00
1. [Appendix](#appendix)
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
## Single Responsibility
2016-09-27 03:58:10 -04:00
Apply the [Single Responsibility Principle](https://wikipedia.org/wiki/Single_responsibility_principle) to all components, services, and other symbols.
This helps make the app cleaner, easier to read and maintain, and more testable.
2016-04-25 12:24:13 -04:00
2016-05-09 15:45:40 -04:00
### <a id="01-01"></a>Rule of One
#### <a href="#01-01">Style 01-01</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** define one thing (e.g. service or component) per file.
.s-rule.consider
2016-04-26 01:42:22 -04:00
:marked
2016-04-28 03:58:50 -04:00
**Consider** limiting files to 400 lines of code.
2016-04-26 01:42:22 -04:00
.s-why
:marked
2016-04-25 12:24:13 -04:00
**Why?** One component per file makes it far easier to read, maintain, and avoid collisions with teams in source control.
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-04-25 12:24:13 -04:00
**Why?** One component per file avoids hidden bugs that often arise when combining components in a file where they may share variables, create unwanted closures, or unwanted coupling with dependencies.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-13 21:51:03 -04:00
**Why?** A single component can be the default export for its file which facilitates lazy loading with the Router.
2016-04-26 01:42:22 -04:00
:marked
2016-04-01 20:23:34 -04:00
The key is to make the code more reusable, easier to read, and less mistake prone.
2016-05-02 12:58:30 -04:00
2016-04-26 01:42:22 -04:00
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*.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/01-01/app/heroes/hero.component.avoid.ts', '', 'app/heroes/hero.component.ts')(avoid=1)
2016-04-01 20:23:34 -04:00
:marked
2016-04-26 01:42:22 -04:00
Better to redistribute the component and supporting activities into their own dedicated files.
2016-04-01 20:23:34 -04:00
+makeTabs(
2016-05-24 09:16:23 -04:00
`style-guide/ts/01-01/main.ts,
2016-08-09 12:38:25 -04:00
style-guide/ts/01-01/app/app.module.ts,
2016-04-26 01:42:22 -04:00
style-guide/ts/01-01/app/app.component.ts,
style-guide/ts/01-01/app/heroes/heroes.component.ts,
2016-04-28 03:58:50 -04:00
style-guide/ts/01-01/app/heroes/shared/hero.service.ts,
2016-05-02 12:58:30 -04:00
style-guide/ts/01-01/app/heroes/shared/hero.model.ts,
2016-04-28 03:58:50 -04:00
style-guide/ts/01-01/app/heroes/shared/mock-heroes.ts`,
2016-04-25 12:24:13 -04:00
'',
2016-05-24 09:16:23 -04:00
`main.ts,
2016-08-09 12:38:25 -04:00
app/app.module.ts,
2016-05-02 12:58:30 -04:00
app/app.component.ts,
app/heroes/heroes.component.ts,
app/heroes/shared/hero.service.ts,
2016-04-28 03:58:50 -04:00
app/heroes/shared/hero.model.ts,
app/heroes/shared/mock-heroes.ts`)
2016-04-25 12:24:13 -04:00
2016-04-01 20:23:34 -04:00
:marked
As the app grows, this rule becomes even more important.
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-01 20:23:34 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="01-02"></a>Small Functions
#### <a href="#01-02">Style 01-02</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** define small functions
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.consider
:marked
**Consider** limiting to no more than 75 lines.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-04-25 12:24:13 -04:00
**Why?** Small functions are easier to test, especially when they do one thing and serve one purpose.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why
:marked
2016-04-25 12:24:13 -04:00
**Why?** Small functions promote reuse.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why
:marked
2016-04-25 12:24:13 -04:00
**Why?** Small functions are easier to read.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why
:marked
2016-04-25 12:24:13 -04:00
**Why?** Small functions are easier to maintain.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
:marked
2016-04-25 12:24:13 -04:00
**Why?** Small functions help avoid hidden bugs that come with large functions that share variables with external scope, create unwanted closures, or unwanted coupling with dependencies.
2016-04-01 20:23:34 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
2016-04-13 17:39:12 -04:00
## Naming
2016-05-02 12:58:30 -04:00
Naming conventions are hugely important to maintainability and readability. This guide recommends naming conventions for the file name and the symbol name.
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-25 12:24:13 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="02-01"></a>General Naming Guidelines
#### <a href="#02-01">Style 02-01</a>
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** use consistent names for all symbols.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** follow a pattern that describes the symbol's feature then its type. The recommended pattern is `feature.type.ts`.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Naming conventions help provide a consistent way to find content at a glance. Consistency within the project is vital. Consistency with a team is important. Consistency across a company provides tremendous efficiency.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** The naming conventions should simply help find desited code faster and make it easier to understand.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Names of folders and files should clearly convey their intent. For example, `app/heroes/hero-list.component.ts` may contain a component that manages a list of heroes.
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="02-02"></a>Separate File Names with Dots and Dashes
#### <a href="#02-02">Style 02-02</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-22 17:34:44 -04:00
**Do** use dashes to separate words in the descriptive name.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** use dots to separate the descriptive name from the type.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-22 17:34:44 -04:00
**Do** use consistent type names for all components following a pattern that describes the component's feature then its type. A recommended pattern is `feature.type.ts`.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** use conventional type names including `.service`, `.component`, `.pipe`, `.module`, `.directive`.
2016-05-22 17:34:44 -04:00
Invent additional type names if you must but take care not to create too many.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-05-22 17:34:44 -04:00
**Why?** Type names provide a consistent way to quickly identify what is in the file.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-05-22 17:34:44 -04:00
**Why?** Make it easy to find a specific file type using an editor or IDE's fuzzy search techniques.
.s-why
:marked
**Why?** Unabbreviated type names such as `.service` are descriptive and unambiguous.
Abbreviations such as `.srv`, `.svc`, and `.serv` can be confusing.
2016-04-28 03:58:50 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Provides pattern matching for any automated tasks.
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-09-27 03:58:10 -04:00
### <a id="02-03"></a>Symbols and File Names
2016-05-09 15:45:40 -04:00
#### <a href="#02-03">Style 02-03</a>
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** use consistent names for all assets named after what they represent.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** use upper camel case for class names. Match the name of the symbol to the name of the file.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** append the symbol name with the conventional suffix for a thing of that type
(e.g., `Component`, `Directive`, `Module`, `Pipe`, `Service`).
2016-04-13 17:39:12 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.do
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 14:49:23 -04:00
**Do** give the filename the conventional suffix for a file of that type
2016-09-27 03:58:10 -04:00
(e.g., `.component.ts`, `.directive.ts`, `.module.ts`, `.pipe.ts`, `.service.ts`).
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** Provides a consistent way to quickly identify and reference assets.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Upper camel case is conventional for identifying objects that can be instantiated using a constructor.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
2016-05-02 12:58:30 -04:00
col(width="50%")
2016-05-01 02:11:33 -04:00
tr
th Symbol Name
th File Name
tr(style=top)
td
code-example.
@Component({ ... })
2016-09-27 03:58:10 -04:00
export class AppComponent { }
2016-05-01 02:11:33 -04:00
td
:marked
app.component.ts
tr(style=top)
td
code-example.
@Component({ ... })
2016-09-27 03:58:10 -04:00
export class HeroesComponent { }
2016-05-01 02:11:33 -04:00
td
:marked
heroes.component.ts
tr(style=top)
td
code-example.
@Component({ ... })
2016-09-27 03:58:10 -04:00
export class HeroListComponent { }
2016-05-01 02:11:33 -04:00
td
:marked
hero-list.component.ts
tr(style=top)
td
code-example.
@Component({ ... })
2016-09-27 03:58:10 -04:00
export class HeroDetailComponent { }
2016-05-01 02:11:33 -04:00
td
:marked
hero-detail.component.ts
tr(style=top)
td
code-example.
@Directive({ ... })
2016-09-27 03:58:10 -04:00
export class ValidationDirective { }
2016-05-01 02:11:33 -04:00
td
:marked
validation.directive.ts
2016-09-27 03:58:10 -04:00
tr(style=top)
td
code-example.
@NgModule({ ... })
export class AppModule
td
:marked
app.module.ts
tr(style=top)
td
code-example.
@Pipe({ name: 'initCaps' })
export class InitCapsPipe implements PipeTransform { }
td
:marked
init-caps.pipe.ts
tr(style=top)
td
code-example.
@Injectable()
export class UserProfileService { }
td
:marked
user-profile.service.ts
2016-04-28 03:58:50 -04:00
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="02-04"></a>Service Names
#### <a href="#02-04">Style 02-04</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** use consistent names for all services named after their feature.
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** use upper camel case for services.
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** suffix services with `Service` when it is not clear what they are (e.g. when they are nouns).
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Provides a consistent way to quickly identify and reference services.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-05-20 13:14:56 -04:00
**Why?** Clear service names such as `Logger` do not require a suffix.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Service names such as `Credit` are nouns and require a suffix and should be named with a suffix when it is not obvious if it is a service or something else.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
2016-05-02 12:58:30 -04:00
col(width="50%")
2016-05-01 02:11:33 -04:00
tr
th Symbol Name
th File Name
tr(style=top)
td
code-example.
@Injectable()
2016-09-27 03:58:10 -04:00
export class HeroDataService { }
2016-05-01 02:11:33 -04:00
td
:marked
hero-data.service.ts
tr(style=top)
td
code-example.
@Injectable()
2016-09-27 03:58:10 -04:00
export class CreditService { }
2016-05-01 02:11:33 -04:00
td
:marked
credit.service.ts
tr(style=top)
td
code-example.
@Injectable()
2016-09-27 03:58:10 -04:00
export class Logger { }
2016-05-01 02:11:33 -04:00
td
:marked
logger.service.ts
2016-04-28 03:58:50 -04:00
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="02-05"></a>Bootstrapping
#### <a href="#02-05">Style 02-05</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** put bootstrapping and platform logic for the app in a file named `main.ts`.
2016-04-25 12:24:13 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.do
:marked
2016-09-30 17:30:20 -04:00
**Do** include error handling in the bootstrapping logic.
2016-09-27 03:58:10 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.avoid
:marked
2016-09-27 03:58:10 -04:00
**Avoid** putting app logic in the `main.ts`. Instead consider placing it in a component or service.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Follows a consistent convention for the startup logic of an app.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-05-02 12:58:30 -04:00
**Why?** Follows a familiar convention from other technology platforms.
2016-04-13 17:39:12 -04:00
2016-09-27 03:58:10 -04:00
+makeExample('style-guide/ts/02-05/main.ts', '', 'main.ts')
:marked
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-25 12:24:13 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="02-06"></a>Directive Selectors
#### <a href="#02-06">Style 02-06</a>
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** Use lower camel case for naming the selectors of directives.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Keeps the names of the properties defined in the directives that are bound to the view consistent with the attribute names.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-19 23:24:40 -04:00
**Why?** The Angular HTML parser is case sensitive and will recognize lower camel case.
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-25 12:24:13 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="02-07"></a>Custom Prefix for Components
#### <a href="#02-07">Style 02-07</a>
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** use a hyphenated, lowercase element selector value (e.g. `admin-users`).
.s-rule.do
:marked
**Do** use a custom prefix for a component selector.
For example, the prefix `toh` represents from **T**our **o**f **H**eroes and the prefix `admin` represents an admin feature area.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** use a prefix that identifies the feature area or the app itself.
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** Prevents element name collisions with components in other apps and with native HTML elements.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-why
2016-05-01 02:11:33 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Makes it easier to promote and share the component in other apps.
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Components are easy to identify in the DOM.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/02-07/app/heroes/hero.component.avoid.ts', 'example', 'app/heroes/hero.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/02-07/app/users/users.component.avoid.ts', 'example', 'app/users/users.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/02-07/app/heroes/hero.component.ts', 'example', 'app/heroes/hero.component.ts')
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/02-07/app/users/users.component.ts', 'example', 'app/users/users.component.ts')
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="02-08"></a>Custom Prefix for Directives
#### <a href="#02-08">Style 02-08</a>
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** use a custom prefix for the selector of directives (e.g, the prefix `toh` from **T**our **o**f **H**eroes).
.s-rule.do
:marked
**Do** spell non-element selectors in lower camel case unless the selector is meant to match a native HTML attribute.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-05-01 02:11:33 -04:00
**Why?** Prevents name collisions.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Directives are easily identified.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/02-08/app/shared/validate.directive.avoid.ts', 'example', 'app/shared/validate.directive.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/02-08/app/shared/validate.directive.ts', 'example', 'app/shared/validate.directive.ts')
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="02-09"></a>Pipe Names
#### <a href="#02-09">Style 02-09</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** use consistent names for all pipes, named after their feature.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Provides a consistent way to quickly identify and reference pipes.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
2016-05-02 12:58:30 -04:00
col(width="50%")
2016-05-01 02:11:33 -04:00
tr
th Symbol Name
th File Name
tr(style=top)
td
code-example.
@Pipe({ name: 'ellipsis' })
export class EllipsisPipe implements PipeTransform { }
td
:marked
ellipsis.pipe.ts
tr(style=top)
td
code-example.
@Pipe({ name: 'initCaps' })
export class InitCapsPipe implements PipeTransform { }
td
:marked
init-caps.pipe.ts
2016-04-28 03:58:50 -04:00
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="02-10"></a>Unit Test File Names
#### <a href="#02-10">Style 02-10</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-01 02:11:33 -04:00
**Do** name test specification files the same as the component they test.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** name test specification files with a suffix of `.spec`.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-04-25 12:24:13 -04:00
**Why?** Provides a consistent way to quickly identify tests.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-04-13 17:39:12 -04:00
**Why?** Provides pattern matching for [karma](http://karma-runner.github.io/) or other test runners.
2016-04-28 03:58:50 -04:00
:marked
2016-05-01 02:11:33 -04:00
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
2016-05-02 12:58:30 -04:00
col(width="50%")
2016-05-01 02:11:33 -04:00
tr
th Symbol Name
th File Name
tr(style=top)
td
:marked
Components
td
:marked
heroes.component.spec.ts
:marked
hero-list.component.spec.ts
:marked
hero-detail.component.spec.ts
tr(style=top)
td
:marked
Services
td
:marked
logger.service.spec.ts
:marked
hero.service.spec.ts
:marked
filter-text.service.spec.ts
tr(style=top)
td
:marked
Pipes
td
:marked
ellipsis.pipe.spec.ts
:marked
init-caps.pipe.spec.ts
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-25 12:24:13 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="02-11"></a>End to End Test File Names
#### <a href="#02-11">Style 02-11</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** name end-to-end test specification files after the feature they test with a suffix of `.e2e-spec`.
.s-why
:marked
**Why?** Provides a consistent way to quickly identify end-to-end tests.
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Provides pattern matching for test runners and build automation.
:marked
2016-05-01 02:11:33 -04:00
:marked
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
2016-05-02 12:58:30 -04:00
col(width="50%")
2016-05-01 02:11:33 -04:00
tr
th Symbol Name
th File Name
tr(style=top)
td
:marked
End to End Tests
td
:marked
app.e2e-spec.ts
:marked
heroes.e2e-spec.ts
:marked
2016-04-28 03:58:50 -04:00
a(href="#toc") Back to top
2016-09-27 03:58:10 -04:00
.l-main-section
:marked
### <a id="02-12"></a>Angular NgModule Names
#### <a href="#02-12">Style 02-12</a>
.s-rule.do
:marked
**Do** append the symbol name with the suffix `Module`.
.s-rule.do
:marked
**Do** give the file name the `.module.ts` extension.
.s-rule.do
:marked
**Do** name the module after the feature and folder it resides in.
.s-why
:marked
**Why?** Provides a consistent way to quickly identify and reference modules.
.s-why
:marked
**Why?** Upper camel case is conventional for identifying objects that can be instantiated using a constructor.
.s-why.s-why-last
:marked
**Why?** Easily identifies the module as the root of the same named feature.
.s-rule.do
:marked
**Do** suffix a _RoutingModule_ class name with `RoutingModule`.
.s-rule.do
:marked
**Do** end the filename of a _RoutingModule_ with `-routing.module.ts`.
.s-why.s-why-last
:marked
**Why?** A `RoutingModule` is a module dedicated exclusively to configuring the Angular router.
A consistent class and file name convention make these modules easy to spot and verify.
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
col(width="50%")
tr
th Symbol Name
th File Name
tr(style=top)
td
code-example.
@NgModule({ ... })
export class AppModule { }
td
:marked
app.module.ts
tr(style=top)
td
code-example.
@NgModule({ ... })
export class HeroesModule { }
td
:marked
heroes.module.ts
tr(style=top)
td
code-example.
@NgModule({ ... })
export class VillainsModule { }
td
:marked
villains.module.ts
tr(style=top)
td
code-example.
@NgModule({ ... })
export class AppRoutingModule { }
td
:marked
app-routing.module.ts
tr(style=top)
td
code-example.
@NgModule({ ... })
export class HeroesRoutingModule { }
td
:marked
heroes-routing.module.ts
:marked
a(href="#toc") Back to top
2016-04-28 03:58:50 -04:00
.l-main-section
:marked
2016-05-01 02:11:33 -04:00
## Coding Conventions
2016-04-28 03:58:50 -04:00
Have consistent set of coding, naming, and whitespace conventions.
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="03-01"></a>Classes
#### <a href="#03-01">Style 03-01</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** use upper camel case when naming classes.
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Follows conventional thinking for class names.
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Classes can be instantiated and construct an instance.
By convention, upper camel case indicates a constructable asset.
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
+makeExample('style-guide/ts/03-01/app/core/exception.service.avoid.ts', 'example', 'app/shared/exception.service.ts')(avoid=1)
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
+makeExample('style-guide/ts/03-01/app/core/exception.service.ts', 'example', 'app/shared/exception.service.ts')
2016-04-28 03:58:50 -04:00
:marked
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="03-02"></a>Constants
#### <a href="#03-02">Style 03-02</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-07-07 18:27:15 -04:00
**Do** declare variables with `const` if their values should not change during the application lifetime.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-07-07 18:27:15 -04:00
**Why?** Conveys to readers that the value is invariant.
2016-04-28 03:58:50 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** TypeScript helps enforce that intent by requiring immediate initialization and by
2016-07-07 18:27:15 -04:00
preventing subsequent re-assignment.
.s-rule.consider
:marked
**Consider** spelling `const` variables in lower camel case.
2016-04-28 03:58:50 -04:00
2016-07-07 18:27:15 -04:00
.s-why
:marked
**Why?** lower camel case variable names (`heroRoutes`) are easier to read and understand
than the traditional UPPER_SNAKE_CASE names (`HERO_ROUTES`).
.s-why.s-why-last
:marked
**Why?** The tradition of naming constants in UPPER_SNAKE_CASE reflects
an era before the modern IDEs that quickly reveal the `const` declaration.
TypeScript itself prevents accidental reassignment.
.s-rule.do
:marked
**Do** tolerate _existing_ `const` variables that are spelled in UPPER_SNAKE_CASE.
.s-why.s-why-last
:marked
2016-09-27 03:58:10 -04:00
**Why?** The tradition of UPPER_SNAKE_CASE remains popular and pervasive,
2016-07-07 18:27:15 -04:00
especially in third party modules.
2016-09-27 03:58:10 -04:00
It is rarely worth the effort to change them or the risk of breaking existing code and documentation.
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
+makeExample('style-guide/ts/03-02/app/core/data.service.ts', '', 'app/shared/data.service.ts')
2016-04-28 03:58:50 -04:00
:marked
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="03-03"></a>Interfaces
#### <a href="#03-03">Style 03-03</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** name an interface using upper camel case.
2016-04-28 03:58:50 -04:00
2016-06-01 18:13:57 -04:00
.s-rule.consider
2016-04-28 03:58:50 -04:00
:marked
2016-05-02 12:58:30 -04:00
**Consider** naming an interface without an `I` prefix.
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.consider
:marked
**Consider** using a class instead of an interface.
.s-why
:marked
**Why?** <a href="https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines" target="_blank">TypeScript guidelines</a>
discourage the "I" prefix.
.s-why
:marked
**Why?** A class alone is less code than a _class-plus-interface_.
.s-why
:marked
**Why?** A class can act as an interface (use `implements` instead of `extends`).
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** An interface-class can be a provider lookup token in Angular dependency injection.
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
+makeExample('style-guide/ts/03-03/app/core/hero-collector.service.avoid.ts', 'example', 'app/shared/hero-collector.service.ts')(avoid=1)
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
+makeExample('style-guide/ts/03-03/app/core/hero-collector.service.ts', 'example', 'app/shared/hero-collector.service.ts')
2016-04-28 03:58:50 -04:00
:marked
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="03-04"></a>Properties and Methods
#### <a href="#03-04">Style 03-04</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** use lower camel case to name properties and methods.
2016-04-28 03:58:50 -04:00
.s-rule.avoid
:marked
2016-05-02 12:58:30 -04:00
**Avoid** prefixing private properties and methods with an underscore.
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Follows conventional thinking for properties and methods.
.s-why
:marked
2016-05-02 12:58:30 -04:00
**Why?** JavaScript lacks a true private property or method.
2016-04-28 03:58:50 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-05-02 12:58:30 -04:00
**Why?** TypeScript tooling makes it easy to identify private vs public properties and methods.
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
+makeExample('style-guide/ts/03-04/app/core/toast.service.avoid.ts', 'example', 'app/shared/toast.service.ts')(avoid=1)
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
+makeExample('style-guide/ts/03-04/app/core/toast.service.ts', 'example', 'app/shared/toast.service.ts')
2016-04-28 03:58:50 -04:00
:marked
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="03-06"></a>Import Line Spacing
#### <a href="#03-06">Style 03-06</a>
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.consider
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Consider** leaving one empty line between third party imports and application imports.
2016-05-02 12:58:30 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.consider
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Consider** listing import lines alphabetized by the module.
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.consider
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Consider** listing destructured imported assets alphabetically.
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-05-02 12:58:30 -04:00
**Why?** The empty line makes it easy to read and locate imports.
2016-04-28 03:58:50 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Alphabetizing makes it easier to read and locate imports.
2016-09-13 21:51:03 -04:00
+makeExample('style-guide/ts/03-06/app/heroes/shared/hero.service.avoid.ts', 'example', 'app/heroes/shared/hero.service.ts')(avoid=1)
2016-04-28 03:58:50 -04:00
:marked
2016-09-13 21:51:03 -04:00
+makeExample('style-guide/ts/03-06/app/heroes/shared/hero.service.ts', 'example', 'app/heroes/shared/hero.service.ts')
2016-04-28 03:58:50 -04:00
:marked
a(href="#toc") Back to top
.l-main-section
:marked
2016-09-27 03:58:10 -04:00
## App Structure and Angular Modules
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
Have a near-term view of implementation and a long-term vision. Start small but keep in mind where the app is heading down the road.
2016-05-02 12:58:30 -04:00
2016-09-27 03:58:10 -04:00
All of the app's code goes in a folder named `app`.
All feature areas are in their own folder, with their own Angular module.
All content is 1 asset per file. Each component, service, and pipe is in its own file.
All 3rd party vendor scripts are stored in another folder and not in the `app` folder.
You didn't write them and you don't want them cluttering app.
Use the naming conventions for files in this guide.
2016-04-28 03:58:50 -04:00
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="04-01"></a>LIFT
#### <a href="#04-01">Style 04-01</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** structure the app such that you can `L`ocate code quickly,
`I`dentify the code at a glance,
keep the `F`lattest structure you can, and
`T`ry to be DRY.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** define the structure to follow these four basic guidelines, listed in order of importance.
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** LIFT Provides a consistent structure that scales well, is modular, and makes it easier to increase developer efficiency by finding code quickly.
To confirm your intuition about a particular structure, ask:
_can I quickly open and start work in all of the related files for this feature_?
2016-04-28 03:58:50 -04:00
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="04-02"></a>Locate
#### <a href="#04-02">Style 04-02</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** make locating code intuitive, simple and fast.
2016-04-28 03:58:50 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?**
To work efficiently you must be able to find files quickly,
especially when you do not know (or do not remember) the file _names_.
Keeping related files near each other in an intuitive location saves time.
A descriptive folder structure makes a world of difference to you and the people who come after you.
2016-04-28 03:58:50 -04:00
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="04-03"></a>Identify
#### <a href="#04-03">Style 04-03</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** name the file such that you instantly know what it contains and represents.
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-03 18:15:00 -04:00
**Do** be descriptive with file names and keep the contents of the file to exactly one component.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.avoid
:marked
2016-05-02 12:58:30 -04:00
**Avoid** files with multiple components, multiple services, or a mixture.
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Spend less time hunting and pecking for code, and become more efficient.
Longer file names are far better than _short-but-obscure_ abbreviated names.
2016-04-28 03:58:50 -04:00
.l-sub-section
:marked
2016-09-27 03:58:10 -04:00
It may be advantageous to deviate from the _one-thing-per-file_ rule when
you have a set of small, closely-related features that are better discovered and understood
in a single file than as multiple files. Be wary of this loophole.
2016-04-28 03:58:50 -04:00
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="04-04"></a>Flat
#### <a href="#04-04">Style 04-04</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** keep a flat folder structure as long as possible.
2016-04-28 03:58:50 -04:00
.s-rule.consider
:marked
2016-09-27 03:58:10 -04:00
**Consider** creating sub-folders when a folder reaches seven or more files.
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.consider
:marked
**Consider** configuring the IDE to hide distracting, irrelevant files such as generated `.js` and `.js.map` files.
s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** No one wants to search for a file through seven levels of folders.
A flat structure is easy to scan.
On the other hand,
<a href="https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two" target="_blank">psychologists believe</a>
that humans start to struggle when the number of adjacent interesting things exceeds nine.
So when a folder has ten or more files, it may be time to create subfolders.
Base your decision on your comfort level.
Use a flatter structure until there is an obvious value to creating a new folder.
2016-04-28 03:58:50 -04:00
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="04-05"></a>T-DRY (Try to be DRY)
#### <a href="#04-05">Style 04-05</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** be DRY (Don't Repeat Yourself)
2016-04-28 03:58:50 -04:00
.s-rule.avoid
:marked
2016-09-27 03:58:10 -04:00
**Avoid** being so DRY that you sacrifice readability.
2016-04-28 03:58:50 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Being DRY is important, but not crucial if it sacrifices the other elements of LIFT.
That's why its calle _T-DRY_.
For example, it's redundant to name a component, `hero-view.component.html` because a component is obviously a view.
But if something is not obvious or departs from a convention, then spell it out.
2016-04-28 03:58:50 -04:00
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="04-06"></a>Overall Structural Guidelines
#### <a href="#04-06">Style 04-06</a>
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** start small but keep in mind where the app is heading down the road.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** have a near term view of implementation and a long term vision.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** put all of the app's code in a folder named `app`.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.consider
:marked
2016-09-27 03:58:10 -04:00
**Consider** creating a folder for a component when is has multiple accompanying files (`.ts`, `.html`, `.css` and `.spec`).
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** Helps keep the app structure small and easy to maintain in the early stages, while being easy to evolve as the app grows.
2016-04-28 03:58:50 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Components often have four files (e.g. `*.html`, `*.css`, `*.ts`, and `*.spec.ts`) and can clutter a folder quickly.
2016-09-27 03:58:10 -04:00
a(id='file-tree')
:marked
Folder and File Structure
2016-04-28 03:58:50 -04:00
.filetree
.file src
.children
.file app
.children
2016-09-27 03:58:10 -04:00
.file core
.children
.file core.module.ts
.file exception.service.ts|spec.ts
.file user-profile.service.ts|spec.ts
2016-09-13 21:51:03 -04:00
.file heroes
2016-04-28 03:58:50 -04:00
.children
.file hero
.children
.file hero.component.ts|html|css|spec.ts
.file hero-list
.children
.file hero-list.component.ts|html|css|spec.ts
.file shared
.children
2016-09-27 03:58:10 -04:00
.file hero-button.component.ts|html|css|spec.ts
2016-04-28 03:58:50 -04:00
.file hero.model.ts
.file hero.service.ts|spec.ts
.file heroes.component.ts|html|css|spec.ts
2016-09-27 03:58:10 -04:00
.file heroes.module.ts
.file heroes-routing.module.ts
2016-04-28 03:58:50 -04:00
.file shared
2016-05-02 12:58:30 -04:00
.children
2016-09-27 03:58:10 -04:00
.file shared.module.ts
.file init-caps.pipe.ts|spec.ts
.file text-filter.component.ts|spec.ts
.file text-filter.service.ts|spec.ts
.file villains
.children
.file villain
.children
.file ...
.file villain-list
.children
.file ...
.file shared
.children
.file ...
.file villains.component.ts|html|css|spec.ts
.file villains.module.ts
.file villains-routing.module.ts
2016-04-28 03:58:50 -04:00
.file app.component.ts|html|css|spec.ts
2016-09-27 03:58:10 -04:00
.file app.module.ts
.file app-routing.module.ts
2016-05-13 16:33:37 -04:00
.file main.ts
2016-04-28 03:58:50 -04:00
.file index.html
.file ...
:marked
.l-sub-section
:marked
2016-09-27 03:58:10 -04:00
While components in dedicated folder are widely preferred,
another option for small apps is to keep components flat (not in a dedicated folder).
This adds up to four files to the existing folder, but also reduces the folder nesting.
Whatever you choose, be consistent.
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-09-27 03:58:10 -04:00
### <a id="04-07"></a>Folders-by-Feature Structure
2016-05-09 15:45:40 -04:00
#### <a href="#04-07">Style 04-07</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** create folders named for the feature area they represent.
2016-04-25 12:24:13 -04:00
2016-09-27 03:58:10 -04:00
.s-why
:marked
**Why?** A developer can locate the code, identify what each file represents at a glance, the structure is as flat as it can be, and there is no repetitive nor redundant names.
.s-why
:marked
**Why?** The LIFT guidelines are all covered.
.s-why
:marked
**Why?** Helps reduce the app from becoming cluttered through organizing the content and keeping them aligned with the LIFT guidelines.
.s-why
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**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.
.s-rule.do
:marked
**Do** create an Angular module for each feature area.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** Angular modules make it easy to lazy load routable features.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Angular modules make it easier to isolate, test, and re-use features.
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
.file-tree-reference
a(href="#file-tree") Refer here to this Folder and File Structure example
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-09-27 03:58:10 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-09-27 03:58:10 -04:00
### <a id="04-08"></a>App Root Module
2016-05-09 15:45:40 -04:00
#### <a href="#04-08">Style 04-08</a>
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** create an Angular module at the root of the application.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** Every app requires at least one Angular module.
.s-rule.consider
:marked
**Consider** naming the root module `app.module.ts`.
.s-why.s-why-last
:marked
**Why?** Makes it easier to locate and identify the root module.
+makeExample('style-guide/ts/04-08/app/app.module.ts', 'example', 'app/app.module.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="04-09"></a>Feature Modules
#### <a href="#04-09">Style 04-09</a>
.s-rule.do
:marked
**Do** create an Angular module for all distinct features in an application (e.g. `Heroes` feature).
.s-rule.do
:marked
**Do** place the feature module in the same named folder as the feature area (.e.g `app/heroes`).
.s-rule.do
:marked
**Do** name the feature module file reflecting the name of the feature area and folder (e.g. `app/heroes/heroes.module.ts`)
.s-rule.do
:marked
**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`)
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** A feature module can expose or hide its implementation from other modules.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** A feature module identifies distinct sets of related components that comprise the feature area.
2016-04-13 17:39:12 -04:00
2016-09-27 03:58:10 -04:00
.s-why
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** A feature module can easily be routed to both eagerly and lazily.
2016-04-25 12:24:13 -04:00
2016-09-27 03:58:10 -04:00
.s-why
:marked
**Why?** A feature module defines clear boundaries between specific functionality and other application features.
2016-04-13 17:39:12 -04:00
2016-09-27 03:58:10 -04:00
.s-why
:marked
**Why?** A feature module helps clarify and make it easier to assign development responsibilities to different teams.
.s-why.s-why-last
:marked
**Why?** A feature module can easily be isolated for testing.
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-09-27 03:58:10 -04:00
### <a id="04-10"></a>Shared Feature Module
#### <a href="#04-10">Style 04-10</a>
.s-rule.do
:marked
**Do** create a feature module named `SharedModule` in a `shared` folder (e.g. `app/shared/shared.module.ts` defines `SharedModule`).
.s-rule.do
:marked
**Do** put common components, directives and pipes that will be used throughout the application by other feature modules in the `SharedModule`, where those assets are expected to share a new instance of themselves (not singletons).
.s-rule.do
:marked
**Do** import all modules required by the assets in the `SharedModule` (e.g. `CommonModule` and `FormsModule`).
.s-why
:marked
**Why?** `SharedModule` will contain components, directives and pipes that may need features from another common module (e.g. `ngFor` in `CommonModule`).
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** declare all components, directives, and pipes in the `SharedModule`.
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 14:49:23 -04:00
**Do** export all symbols from the `SharedModule` that other feature modules need to use.
2016-09-27 03:58:10 -04:00
.s-why
:marked
**Why?** `SharedModule` exists to make commonly used components, directives and pipes available for use in the templates of components in many other modules.
.s-rule.avoid
:marked
**Avoid** specifying app-wide singleton providers in a `SharedModule`. Intentional singletons are OK. Take care.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** A lazy loaded feature module that imports that shared module will make its own copy of the service and likely have undesireable results.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** You don't want each module to have its own separate instance of singleton services.
Yet there is a real danger of that happening if the `SharedModule` provides a service.
2016-04-28 03:58:50 -04:00
.filetree
.file src
.children
.file app
.children
.file shared
2016-05-02 12:58:30 -04:00
.children
2016-09-27 03:58:10 -04:00
.file shared.module.ts
.file init-caps.pipe.ts|spec.ts
.file text-filter.component.ts|spec.ts
.file text-filter.service.ts|spec.ts
2016-04-28 03:58:50 -04:00
.file app.component.ts|html|css|spec.ts
2016-09-27 03:58:10 -04:00
.file app.module.ts
.file app-routing.module.ts
2016-05-13 16:33:37 -04:00
.file main.ts
2016-04-28 03:58:50 -04:00
.file index.html
.file ...
:marked
2016-04-13 17:39:12 -04:00
2016-09-27 03:58:10 -04:00
+makeTabs(
`style-guide/ts/04-10/app/shared/shared.module.ts,
style-guide/ts/04-10/app/shared/init-caps.pipe.ts,
style-guide/ts/04-10/app/shared/filter-text/filter-text.component.ts,
style-guide/ts/04-10/app/shared/filter-text/filter-text.service.ts,
style-guide/ts/04-10/app/heroes/heroes.component.ts,
style-guide/ts/04-10/app/heroes/heroes.component.html,
`,
`,,,`,
`app/shared/shared.module.ts,
app/shared/init-caps.pipe.ts,
app/shared/filter-text/filter-text.component.ts,
app/shared/filter-text/filter-text.service.ts,
app/heroes/heroes.component.ts,
app/heroes/heroes.component.html,
`)
:marked
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-09-27 03:58:10 -04:00
### <a id="04-11"></a>Core Feature Module
#### <a href="#04-11">Style 04-11</a>
2016-04-13 17:39:12 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.do
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Do** collect single-use classes and hiding their gory details inside `CoreModule`. A simplified root `AppModule` imports `CoreModule` in its capacity as orchestrator of the application as a whole.
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.do
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Do** create a feature module named `CoreModule` in a `core` folder (e.g. `app/core/core.module.ts` defines `CoreModule`).
.s-rule.do
:marked
**Do** put a singleton service whose instance wil be shared throughout the application in the `CoreModule` (e.g. `ExceptionService` and `LoggerService`).
.s-rule.do
:marked
**Do** import all modules required by the assets in the `CoreModule` (e.g. `CommonModule` and `FormsModule`).
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** `CoreModule` provides one or more singleton services. Angular registers the providers with the app root injector, making a singleton instance of each service available to any component that needs them, whether that component is eagerly or lazily loaded.
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** `CoreModule` will contain singleton services. When a lazy loaded module imports these, it will get a new instance and not the intended app-wide singleton.
.s-rule.do
:marked
**Do** gather application-wide, single use components in the `CoreModule`.
Import it once (in the `AppModule`) when the app starts and never import it anywhere else. (e.g. `NavComponent` and `SpinnerComponent`).
2016-05-23 09:39:55 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** Real world apps can have several single-use components (e.g., spinners, message toasts, and modal dialogs) that appear only in the `AppComponent` template.
They are not imported elsewhere so they're not shared in that sense.
Yet they're too big and messy to leave loose in the root folder.
.s-rule.avoid
:marked
**Avoid** importing the `CoreModule` anywhere except in the `AppModule`.
.s-why
:marked
**Why?** A lazily loaded feature module that directly imports the `CoreModule` will make its own copy of services and likely have undesireable results.
2016-05-23 09:39:55 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** An eagerly loaded feature module already has access to the `AppModule`'s injector, and thus the `CoreModule`'s services.
.s-rule.do
:marked
**Do** export all symbols that from the `CoreModule` that the `AppModule` will import and make available for other feature modules to use.
.s-why
:marked
**Why?** `CoreModule` exists to make commonly used singleton services available for use in the many other modules.
2016-04-28 03:58:50 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** You wnat the entire app to use the one, singleton instance.
You don't want each module to have its own separate instance of singleton services.
Yet there is a real danger of that happening accidentally if the `CoreModule` provides a service.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.filetree
.file src
.children
.file app
.children
2016-09-27 03:58:10 -04:00
.file core
2016-04-28 03:58:50 -04:00
.children
2016-09-27 03:58:10 -04:00
.file core.module.ts
.file logger.service.ts|spec.ts
2016-04-28 03:58:50 -04:00
.file nav
.children
2016-09-27 03:58:10 -04:00
.file nav.component.ts|html|css|spec.ts
.file spinner
2016-04-28 03:58:50 -04:00
.children
2016-09-27 03:58:10 -04:00
.file spinner.component.ts|html|css|spec.ts
.file spinner.service.ts|spec.ts
2016-04-28 03:58:50 -04:00
.file app.component.ts|html|css|spec.ts
2016-09-27 03:58:10 -04:00
.file app.module.ts
.file app-routing.module.ts
2016-05-13 16:33:37 -04:00
.file main.ts
2016-04-28 03:58:50 -04:00
.file index.html
.file ...
:marked
2016-09-27 03:58:10 -04:00
+makeTabs(
`
style-guide/ts/04-11/app/app.module.ts,
style-guide/ts/04-11/app/core/core.module.ts,
style-guide/ts/04-11/app/core/logger.service.ts,
style-guide/ts/04-11/app/core/nav/nav.component.ts,
style-guide/ts/04-11/app/core/nav/nav.component.html,
style-guide/ts/04-11/app/core/spinner/spinner.component.ts,
style-guide/ts/04-11/app/core/spinner/spinner.component.html,
style-guide/ts/04-11/app/core/spinner/spinner.service.ts
`,
`example,,,,,,,`,
`
app/app.module.ts,
app/core/core.module.ts,
app/core/logger.service.ts,
app/core/nav/nav.component.ts,
app/core/nav/nav.component.html,
app/core/spinner/spinner.component.ts,
app/core/spinner/spinner.component.html,
app/core/spinner/spinner.service.ts
`)
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
.l-sub-section
:marked
`AppModule` is a little smaller because many app/root classes have moved to other modules.
`AppModule` is stable because you will add future components and providers to other modules, not this one.
`AppModule` delegates to imported modules rather than doing work.
`AppModule` is focused on its main task, orchestrating the app as a whole.
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-09-27 03:58:10 -04:00
### <a id="04-12"></a>Prevent Reimport of Core Module
#### <a href="#04-12">Style 04-12</a>
Only the root `AppModule` should import the `CoreModule`.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** guard against reimporting of `CoreModule` and fail fast by adding guard logic.
.s-why.s-why
:marked
**Why?** Guards against reimporting of the `CoreModule`.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Guards against creating multiple instances of assets intended to be singletons.
+makeTabs(
`
style-guide/ts/04-12/app/core/module-import-guard.ts,
style-guide/ts/04-12/app/core/core.module.ts
`,
`,`,
`
app/core/module-import-guard,
app/core/core.module.ts
`)
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
:marked
2016-09-27 03:58:10 -04:00
### <a id="04-13"></a>Lazy Loaded Folders
2016-05-09 15:45:40 -04:00
#### <a href="#04-13">Style 04-13</a>
2016-09-27 03:58:10 -04:00
A distinct application feature or workflow may be *lazy loaded* or *loaded on demand* rather than when the application starts.
2016-04-28 03:58:50 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.do
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Do** put the contents of lazy loaded features in a *lazy loaded folder*.
A typical *lazy loaded folder* contains a *routing component*, its child components, and their related assets and modules.
2016-04-26 01:42:22 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** The folder makes it easy to identify and isolate the feature content.
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-09-27 03:58:10 -04:00
### <a id="04-14"></a>Never Directly Import Lazy Loaded Folders
2016-05-09 15:45:40 -04:00
#### <a href="#04-14">Style 04-14</a>
2016-04-13 17:39:12 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.avoid
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Avoid** allowing modules in sibling and parent folders to directly import a module in a *lazy loaded feature*.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Directly importing and using a module will load it immediately when the intention is to load it on demand.
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
## Components
2016-09-27 03:58:10 -04:00
### <a id="05-02"></a>Component Selector Naming
2016-05-09 15:45:40 -04:00
#### <a href="#05-02">Style 05-02</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** use _dashed-case_ or _kebab-case_ for naming the element selectors of components.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Keeps the element names consistent with the specification for [Custom Elements](https://www.w3.org/TR/custom-elements/).
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/05-02/app/heroes/shared/hero-button/hero-button.component.avoid.ts', 'example', 'app/heroes/shared/hero-button/hero-button.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
+makeTabs(
2016-04-28 03:58:50 -04:00
`style-guide/ts/05-02/app/heroes/shared/hero-button/hero-button.component.ts,
2016-04-26 01:42:22 -04:00
style-guide/ts/05-02/app/app.component.html`,
'example,',
2016-05-02 12:58:30 -04:00
`app/heroes/shared/hero-button/hero-button.component.ts,
2016-04-26 01:42:22 -04:00
app/app.component.html`)
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="05-03"></a>Components as Elements
#### <a href="#05-03">Style 05-03</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** define components as elements via the selector.
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** components have templates containing HTML and optional Angular template syntax. They are most associated with putting content on a page, and thus are more closely aligned with elements.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** A component represents a visual element on the page.
Defining the selector as an HTML element tag is consistent with native HTML elements and WebComponents.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** It is easier to recognize that a symbol is a component vs a directive by looking at the template's html.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/05-03/app/heroes/shared/hero-button/hero-button.component.avoid.ts', 'example', 'app/heroes/hero-button/hero-button.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-05-23 09:52:01 -04:00
+makeExample('style-guide/ts/05-03/app/app.component.avoid.html', '', 'app/app.component.html')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
+makeTabs(
2016-04-28 03:58:50 -04:00
`style-guide/ts/05-03/app/heroes/shared/hero-button/hero-button.component.ts,
2016-04-26 01:42:22 -04:00
style-guide/ts/05-03/app/app.component.html`,
'example,',
2016-05-02 12:58:30 -04:00
`app/heroes/shared/hero-button/hero-button.component.ts,
2016-04-26 01:42:22 -04:00
app/app.component.html`)
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="05-04"></a>Extract Template and Styles to Their Own Files
#### <a href="#05-04">Style 05-04</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** extract templates and styles into a separate file, when more than 3 lines.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** name the template file `[component-name].component.html`, where [component-name] is the component name.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** name the style file `[component-name].component.css`, where [component-name] is the component name.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Syntax hints for inline templates in (*.js and *.ts) code files are not supported by some editors.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** A component file's logic is easier to read when not mixed with inline template and styles.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/05-04/app/heroes/heroes.component.avoid.ts', 'example', 'app/heroes/heroes.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
+makeTabs(
`style-guide/ts/05-04/app/heroes/heroes.component.ts,
style-guide/ts/05-04/app/heroes/heroes.component.html,
style-guide/ts/05-04/app/heroes/heroes.component.css`,
'example,,',
2016-05-02 12:58:30 -04:00
`app/heroes/heroes.component.ts,
app/heroes/heroes.component.html,
2016-04-26 01:42:22 -04:00
app/heroes/heroes.component.css`)
:marked
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="05-12"></a>Decorate Input and Output Properties Inline
#### <a href="#05-12">Style 05-12</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-06-21 10:22:38 -04:00
**Do** use [`@Input`](https://angular.io/docs/ts/latest/api/core/index/Input-var.html) and [`@Output`](https://angular.io/docs/ts/latest/api/core/index/Output-var.html) instead of the `inputs` and `outputs` properties of the [`@Directive`](https://angular.io/docs/ts/latest/api/core/index/Directive-decorator.html) and [`@Component`](https://angular.io/docs/ts/latest/api/core/index/Component-decorator.html) decorators:
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** place the `@Input()` or `@Output()` on the same line as the property they decorate.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-05-01 02:11:33 -04:00
**Why?** It is easier and more readable to identify which properties in a class are inputs or outputs.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** If you ever need to rename the property or event name associated to
[`@Input`](https://angular.io/docs/ts/latest/api/core/index/Input-var.html) or
[`@Output`](https://angular.io/docs/ts/latest/api/core/index/Output-var.html)
you can modify it on a single place.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** The metadata declaration attached to the directive is shorter and thus more readable.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Placing the decorator on the same line makes for shorter code and still easily identifies the property as an input or output.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/05-12/app/heroes/shared/hero-button/hero-button.component.avoid.ts', 'example', 'app/heroes/shared/hero-button/hero-button.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/05-12/app/heroes/shared/hero-button/hero-button.component.ts', 'example', 'app/heroes/shared/hero-button/hero-button.component.ts')
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="05-13"></a>Avoid Renaming Inputs and Outputs
#### <a href="#05-13">Style 05-13</a>
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.avoid
:marked
**Avoid** renaming inputs and outputs, when possible.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** May lead to confusion when the output or the input properties of a given directive are named a given way but exported differently as a public API.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/05-13/app/heroes/shared/hero-button/hero-button.component.avoid.ts', 'example', 'app/heroes/shared/hero-button/hero-button.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/05-13/app/app.component.avoid.html', '', 'app/app.component.html')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
+makeTabs(
2016-04-28 03:58:50 -04:00
`style-guide/ts/05-13/app/heroes/shared/hero-button/hero-button.component.ts,
2016-04-26 01:42:22 -04:00
style-guide/ts/05-13/app/app.component.html`,
'example,',
2016-05-02 12:58:30 -04:00
`app/heroes/shared/hero-button/hero-button.component.ts,
2016-04-26 01:42:22 -04:00
app/app.component.html`)
:marked
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="05-14"></a>Member Sequence
#### <a href="#05-14">Style 05-14</a>
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** place properties up top followed by methods.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** place private members after public members, alphabetized.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Placing members in a consistent sequence makes it easy to read and
helps instantly identify which members of the component serve which purpose.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/05-14/app/shared/toast/toast.component.avoid.ts', 'example', 'app/shared/toast/toast.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/05-14/app/shared/toast/toast.component.ts', 'example', 'app/shared/toast/toast.component.ts')
2016-04-26 01:42:22 -04:00
:marked
2016-04-01 20:23:34 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="05-15"></a>Put Logic in Services
#### <a href="#05-14">Style 05-15</a>
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** limit logic in a component to only that required for the view. All other logic should be delegated to services.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** move reusable logic to services and keep components simple and focused on their intended purpose.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Logic may be reused by multiple components when placed within a service and exposed via a function.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Logic in a service can more easily be isolated in a unit test, while the calling logic in the component can be easily mocked.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Removes dependencies and hides implementation details from the component.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Keeps the component slim, trim, and focused.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/05-15/app/heroes/hero-list/hero-list.component.avoid.ts', '', 'app/heroes/hero-list/hero-list.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/05-15/app/heroes/hero-list/hero-list.component.ts', 'example', 'app/heroes/hero-list/hero-list.component.ts')
2016-04-26 01:42:22 -04:00
:marked
2016-04-01 20:23:34 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="05-16"></a>Don't Prefix Output Properties
#### <a href="#05-16">Style 05-16</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** name events without the prefix `on`.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** name event handler methods with the prefix `on` followed by the event name.
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** This is consistent with built-in events such as button clicks.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Angular allows for an [alternative syntax](https://angular.io/docs/ts/latest/guide/template-syntax.html#!#binding-syntax) `on-*`. If the event itself was prefixed with `on` this would result in an `on-onEvent` binding expression.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/05-16/app/heroes/hero.component.avoid.ts', 'example', 'app/heroes/hero.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/05-16/app/app.component.avoid.html', '', 'app/app.component.html')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
+makeTabs(
`style-guide/ts/05-16/app/heroes/hero.component.ts,
style-guide/ts/05-16/app/app.component.html`,
'example,',
2016-05-02 12:58:30 -04:00
`app/heroes/hero.component.ts,
2016-04-26 01:42:22 -04:00
app/app.component.html`)
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="05-17"></a>Put Presentation Logic in the Component Class
#### <a href="#05-17">Style 05-17</a>
2016-04-13 17:39:12 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-27 03:58:10 -04:00
**Do** put presentation logic in the component class, and not in the template.
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** Logic will be contained in one place (the component class) instead of being spread in two places.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-05-02 12:58:30 -04:00
**Why?** Keeping the component's presentation logic in the class instead of the template improves testability, maintainability, and reusability.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/05-17/app/heroes/hero-list/hero-list.component.avoid.ts', 'example', 'app/heroes/hero-list/hero-list.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/05-17/app/heroes/hero-list/hero-list.component.ts', 'example', 'app/heroes/hero-list/hero-list.component.ts')
2016-04-26 01:42:22 -04:00
:marked
2016-04-13 17:39:12 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-13 17:39:12 -04:00
:marked
## Directives
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-13 17:39:12 -04:00
2016-05-02 12:58:30 -04:00
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="06-01"></a>Use Directives to Enhance an Existing Element
#### <a href="#06-01">Style 06-01</a>
2016-05-02 12:58:30 -04:00
.s-rule.do
:marked
**Do** use attribute directives when you have presentation logic without a template.
.s-why
:marked
**Why?** Attributes directives don't have an associated template.
.s-why.s-why-last
:marked
**Why?** An element may have more than one attribute directive applied.
+makeExample('style-guide/ts/06-01/app/shared/highlight.directive.ts', 'example', 'app/shared/highlight.directive.ts')
:marked
+makeExample('style-guide/ts/06-01/app/app.component.html', null, 'app/app.component.html')
:marked
a(href="#toc") Back to top
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-25 12:24:13 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="06-03"></a>Use HostListener and HostBinding Class Decorators
#### <a href="#06-03">Style 06-03</a>
2016-04-13 17:39:12 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.consider
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Consider** preferring the `@HostListener` and `@HostBinding` to the
`host` property of the `@Directive` and `@Component` decorators.
2016-04-13 17:39:12 -04:00
2016-09-27 03:58:10 -04:00
.s-rule.do
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Do** be consistent in your choice.
2016-04-13 17:39:12 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** The property associated with `@HostBinding` or the method associated with `@HostListener`
can be modified only in a single place - in the directive's class.
If you use the `host` metadata property, you must modify both the property declaration inside the controller,
and the metadata associated with the directive.
2016-04-25 12:24:13 -04:00
2016-09-27 03:58:10 -04:00
+makeExample('style-guide/ts/06-03/app/shared/validator.directive.ts', '', 'app/shared/validator.directive.ts')
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
Compare with the less preferred `host` metadata alternative.
.s-why.s-why-last
:marked
**Why?** The `host` metadata is only one term to remember and doesn't require extra ES imports.
2016-04-01 20:23:34 -04:00
2016-09-27 03:58:10 -04:00
+makeExample('style-guide/ts/06-03/app/shared/validator2.directive.ts', '', 'app/shared/validator2.directive.ts')
2016-04-28 03:58:50 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
## Services
2016-09-27 03:58:10 -04:00
### <a id="07-01"></a>Services are Singletons within an Injector
2016-05-09 15:45:40 -04:00
#### <a href="#07-01">Style 07-01</a>
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** use services as singletons within the same injector. Use them for sharing data and functionality.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** Services are ideal for sharing methods across a feature area or an app.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Services are ideal for sharing stateful in-memory data.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/07-01/app/heroes/shared/hero.service.ts', 'example', 'app/heroes/shared/hero.service.ts')
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-25 12:24:13 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="07-02"></a>Single Responsibility
#### <a href="#07-02">Style 07-02</a>
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** create services with a single responsibility that is encapsulated by its context.
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** create a new service once the service begins to exceed that singular purpose.
2016-05-02 12:58:30 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** When a service has multiple responsibilities, it becomes difficult to test.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** When a service has multiple responsibilities, every component or service that injects it now carries the weight of them all.
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-01 20:23:34 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="07-03"></a>Providing a Service
#### <a href="#07-03">Style 07-03</a>
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-09-19 23:24:40 -04:00
**Do** provide services to the Angular injector at the top-most component where they will be shared.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-19 23:24:40 -04:00
**Why?** The Angular injector is hierarchical.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** When providing the service to a top level component, that instance is shared and available to all child components of that top level component.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-05-02 12:58:30 -04:00
**Why?** This is ideal when a service is sharing methods or state.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** This is not ideal when two different components need different instances of a service. In this scenario it would be better to provide the service at the component level that needs the new and separate instance.
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
+makeTabs(
`style-guide/ts/07-03/app/app.component.ts,
2016-04-28 03:58:50 -04:00
style-guide/ts/07-03/app/heroes/hero-list/hero-list.component.ts`,
2016-04-26 01:42:22 -04:00
'',
2016-05-02 12:58:30 -04:00
`app/app.component.ts,
2016-04-28 03:58:50 -04:00
app/heroes/hero-list/hero-list.component.ts`)
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="07-04"></a>Use the @Injectable() Class Decorator
#### <a href="#07-04">Style 07-04</a>
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** use the `@Injectable` class decorator instead of the `@Inject` parameter decorator when using types as tokens for the dependencies of a service.
2016-04-25 12:24:13 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
2016-09-27 03:58:10 -04:00
**Why?** The Angular DI mechanism resolves all dependencies of services based on their types declared with the services' constructors.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** When a service accepts only dependencies associated with type tokens, the `@Injectable()` syntax is much less verbose compared to using `@Inject()` on each individual constructor parameter.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/07-04/app/heroes/shared/hero-arena.service.avoid.ts', 'example', 'app/heroes/shared/hero-arena.service.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/07-04/app/heroes/shared/hero-arena.service.ts', 'example', 'app/heroes/shared/hero-arena.service.ts')
2016-04-26 01:42:22 -04:00
:marked
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
## Data Services
2016-05-09 15:45:40 -04:00
### <a id="08-01"></a>Separate Data Calls
#### <a href="#08-01">Style 08-01</a>
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
2016-05-02 12:58:30 -04:00
**Do** refactor logic for making data operations and interacting with data to a service.
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** make data services responsible for XHR calls, local storage, stashing in memory, or any other data operations.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** The component's responsibility is for the presentation and gathering of information for the view. It should not care how it gets the data, just that it knows who to ask for it. Separating the data services moves the logic on how to get it to the data service, and lets the component be simpler and more focused on the view.
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-why
:marked
**Why?** This makes it easier to test (mock or real) the data calls when testing a component that uses a data service.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
**Why?** Data service implementation may have very specific code to handle the data repository. This may include headers, how to talk to the data, or other services such as `Http`. Separating the logic into a data service encapsulates this logic in a single place hiding the implementation from the outside consumers (perhaps a component), also making it easier to change the implementation.
2016-04-01 20:23:34 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
2016-04-13 17:39:12 -04:00
## Lifecycle Hooks
2016-04-25 12:24:13 -04:00
2016-04-13 17:39:12 -04:00
Use Lifecycle Hooks to tap into important events exposed by Angular.
2016-04-01 20:23:34 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
2016-05-09 15:45:40 -04:00
### <a id="09-01"></a>Implement Lifecycle Hooks Interfaces
#### <a href="#09-01">Style 09-01</a>
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
.s-rule.do
:marked
**Do** implement the lifecycle hook interfaces.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-why.s-why-last
2016-04-28 03:58:50 -04:00
:marked
2016-09-27 03:58:10 -04:00
**Why?** Strongly-typed method signatures.
The compiler and editor can call out misspellings.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
+makeExample('style-guide/ts/09-01/app/heroes/shared/hero-button/hero-button.component.avoid.ts', 'example', 'app/heroes/shared/hero-button/hero-button.component.ts')(avoid=1)
2016-04-26 01:42:22 -04:00
:marked
2016-04-01 20:23:34 -04:00
2016-04-28 03:58:50 -04:00
+makeExample('style-guide/ts/09-01/app/heroes/shared/hero-button/hero-button.component.ts', 'example', 'app/heroes/shared/hero-button/hero-button.component.ts')
2016-04-26 01:42:22 -04:00
:marked
2016-04-01 20:23:34 -04:00
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-04-26 01:42:22 -04:00
.l-main-section
2016-04-01 20:23:34 -04:00
:marked
## Appendix
2016-09-19 23:24:40 -04:00
Useful tools and tips for Angular.
2016-05-01 02:11:33 -04:00
a(href="#toc") Back to top
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="A-01"></a>Codelyzer
#### <a href="#A-01">Style A-01</a>
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-rule.do
:marked
**Do** use [codelyzer](https://www.npmjs.com/package/codelyzer) to follow this guide.
2016-04-01 20:23:34 -04:00
2016-05-01 02:11:33 -04:00
.s-rule.consider
:marked
**Consider** adjusting the rules in codelyzer to suit your needs.
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
a(href="#toc") Back to top
2016-04-25 12:24:13 -04:00
2016-05-01 02:11:33 -04:00
.l-main-section
:marked
2016-05-09 15:45:40 -04:00
### <a id="A-02"></a>File Templates and Snippets
#### <a href="#A-02">Style A-02</a>
2016-05-01 02:11:33 -04:00
.s-rule.do
:marked
**Do** use file templates or snippets to help follow consistent styles and patterns. Here are templates and/or snippets for some of the web development editors and IDEs.
.s-rule.consider
:marked
2016-05-02 12:58:30 -04:00
**Consider** using [snippets](https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2) for [Visual Studio Code](https://code.visualstudio.com/) that follow these styles and guidelines.
2016-05-07 14:19:09 -04:00
2016-05-12 03:16:03 -04:00
<a href="https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2">
2016-09-15 20:18:41 -04:00
<img src="https://github.com/johnpapa/vscode-angular2-snippets/raw/master/images/use-extension.gif" width="80%" alt="Use Extension">
2016-05-12 03:16:03 -04:00
</a>
2016-05-07 14:19:09 -04:00
2016-05-05 11:47:04 -04:00
**Consider** using [snippets](https://atom.io/packages/angular-2-typescript-snippets) for [Atom](https://atom.io/) that follow these styles and guidelines.
2016-05-01 02:11:33 -04:00
2016-05-07 14:19:09 -04:00
**Consider** using [snippets](https://github.com/orizens/sublime-angular2-snippets) for [Sublime Text](http://www.sublimetext.com/) that follow these styles and guidelines.
2016-04-01 20:23:34 -04:00
2016-05-13 08:44:17 -04:00
**Consider** using [snippets](https://github.com/mhartington/vim-angular2-snippets) for [Vim](http://www.vim.org/) that follow these styles and guidelines.
2016-04-26 01:42:22 -04:00
a(href="#toc") Back to top