2017-02-22 13:09:39 -05:00
@title
Style Guide
@intro
Write Angular with style.
@description
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Looking for an opinionated guide to Angular syntax, conventions, and application structure?
Step right in!
2017-03-27 11:08:53 -04:00
This style guide presents preferred conventions and, as importantly, explains why.
{@a toc}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
# Contents
* [Single responsibility ](guide/style-guide#single-responsibility )
* [Naming ](guide/style-guide#naming )
* [Coding conventions ](guide/style-guide#coding-conventions )
* [App structure and Angular modules ](guide/style-guide#application-structure-and-angular-modules )
* [Components ](guide/style-guide#components )
* [Directives ](guide/style-guide#directives )
* [Services ](guide/style-guide#services )
* [Data services ](guide/style-guide#data-services )
* [Lifecycle hooks ](guide/style-guide#lifecycle-hooks )
* [Appendix ](guide/style-guide#appendix )
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Style vocabulary
Each guideline describes either a good or bad practice, and all have a consistent presentation.
The wording of each guideline indicates how strong the recommendation is.
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05: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.
On the other hand, you need a really unusual case for breaking a *Do* guideline.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** guidelines should generally be followed.
If you fully understand the meaning behind the guideline and have a good reason to deviate, then do so. Please strive to be consistent.
~~~
~~~ {.s-rule.avoid}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Avoid** indicates something you should almost never do. Code examples to *avoid* have an unmistakeable red header.
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** gives reasons for following the previous recommendations.
~~~
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
## File structure conventions
Some code examples display a file that has one or more similarly named companion files.
For example, `hero.component.ts` and `hero.component.html` .
2017-03-27 11:08:53 -04:00
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.
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
{@a single-responsibility}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Single responsibility
Apply the
2017-03-27 11:08:53 -04:00
< a href = "https://wikipedia.org/wiki/Single_responsibility_principle" target = "_blank" > < i > single responsibility principle< / i > (SRP)< / a >
2017-02-22 13:09:39 -05:00
to all components, services, and other symbols.
This helps make the app cleaner, easier to read and maintain, and more testable.
2017-03-31 19:57:13 -04:00
### {@a 01-01}Rule of One
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
#### <a href="#01-01">Style 01-01</a>
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** define one thing, such as a service or component, per file.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** limiting files to 400 lines of code.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** One component per file makes it far easier to read, maintain, and avoid
collisions with teams in source control.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
~~~ {.s-why}
**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.
~~~
~~~ {.s-why-last}
**Why?** A single component can be the default export for its file which facilitates lazy loading with the router.
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
The key is to make the code more reusable, easier to read, and less mistake prone.
2017-03-27 11:08:53 -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*.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/01-01/app/heroes/hero.component.avoid.ts" title = "app/heroes/hero.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
It is a better practice to redistribute the component and its
supporting classes into their own, dedicated files.
2017-03-27 11:08:53 -04:00
< code-tabs >
< code-pane title = "main.ts" path = "style-guide/src/01-01/main.ts" >
< / code-pane >
< code-pane title = "app/app.module.ts" path = "style-guide/src/01-01/app/app.module.ts" >
< / code-pane >
< code-pane title = "app/app.component.ts" path = "style-guide/src/01-01/app/app.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/heroes/heroes.component.ts" path = "style-guide/src/01-01/app/heroes/heroes.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/heroes/shared/hero.service.ts" path = "style-guide/src/01-01/app/heroes/shared/hero.service.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/heroes/shared/hero.model.ts" path = "style-guide/src/01-01/app/heroes/shared/hero.model.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/heroes/shared/mock-heroes.ts" path = "style-guide/src/01-01/app/heroes/shared/mock-heroes.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
As the app grows, this rule becomes even more important.
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 01-02}Small functions
2017-02-22 13:09:39 -05:00
#### <a href="#01-02">Style 01-02</a>
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** define small functions
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** limiting to no more than 75 lines.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Small functions are easier to test, especially when they do one thing and serve one purpose.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** Small functions promote reuse.
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~
~~~ {.s-why}
**Why?** Small functions are easier to read.
~~~
~~~ {.s-why}
**Why?** Small functions are easier to maintain.
~~~
~~~ {.s-why-last}
**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.
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Naming
Naming conventions are hugely important to maintainability and readability. This guide recommends naming conventions for the file name and the symbol name.
2017-03-31 19:57:13 -04:00
### {@a 02-01}General Naming Guidelines
2017-02-22 13:09:39 -05:00
#### <a href="#02-01">Style 02-01</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use consistent names for all symbols.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** follow a pattern that describes the symbol's feature then its type. The recommended pattern is `feature.type.ts` .
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**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.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** The naming conventions should simply help find desired code faster and make it easier to understand.
~~~
~~~ {.s-why-last}
**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.
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 02-02}Separate file names with dots and dashes
2017-02-22 13:09:39 -05:00
#### <a href="#02-02">Style 02-02</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use dashes to separate words in the descriptive name.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use dots to separate the descriptive name from the type.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05: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` .
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use conventional type names including `.service` , `.component` , `.pipe` , `.module` , and `.directive` .
Invent additional type names if you must but take care not to create too many.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Type names provide a consistent way to quickly identify what is in the file.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Type names make it easy to find a specific file type using an editor or IDE's fuzzy search techniques.
~~~
~~~ {.s-why}
**Why?** Unabbreviated type names such as `.service` are descriptive and unambiguous.
Abbreviations such as `.srv` , `.svc` , and `.serv` can be confusing.
~~~
~~~ {.s-why-last}
**Why?** Type names provide pattern matching for any automated tasks.
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 02-03}Symbols and file names
2017-02-22 13:09:39 -05:00
#### <a href="#02-03">Style 02-03</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use consistent names for all assets named after what they represent.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use upper camel case for class names.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** match the name of the symbol to the name of the file.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** append the symbol name with the conventional suffix (such as `Component` ,
`Directive` , `Module` , `Pipe` , or `Service` ) for a thing of that type.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** give the filename the conventional suffix (such as `.component.ts` , `.directive.ts` ,
`.module.ts` , `.pipe.ts` , or `.service.ts` ) for a file of that type.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** Consistent conventions make it easy to quickly identify
and reference assets of different types.
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
Symbol Name
< / th >
< th >
File Name
< / th >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Component ({ ... })
export class AppComponent { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
app.component.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Component ({ ... })
export class HeroesComponent { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
heroes.component.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Component ({ ... })
export class HeroListComponent { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
hero-list.component.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Component ({ ... })
export class HeroDetailComponent { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
hero-detail.component.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Directive ({ ... })
export class ValidationDirective { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
validation.directive.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@NgModule ({ ... })
export class AppModule
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
app.module.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Pipe ({ name: 'initCaps' })
export class InitCapsPipe implements PipeTransform { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
init-caps.pipe.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Injectable ()
export class UserProfileService { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
user-profile.service.ts
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 02-04}Service names
2017-02-22 13:09:39 -05:00
#### <a href="#02-04">Style 02-04</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use consistent names for all services named after their feature.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** suffix a service class name with `Service` .
2017-02-22 13:09:39 -05:00
For example, something that gets data or heroes
should be called a `DataService` or a `HeroService` .
A few terms are unambiguously services. They typically
2017-03-27 11:08:53 -04:00
indicate agency by ending in "-er". You may prefer to name
2017-02-22 13:09:39 -05:00
a service that logs messages `Logger` rather than `LoggerService` .
Decide if this exception is agreeable in your project.
As always, strive for consistency.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Provides a consistent way to quickly identify and reference services.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
~~~ {.s-why}
**Why?** Clear service names such as `Logger` do not require a suffix.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**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.
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
Symbol Name
< / th >
< th >
File Name
< / th >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Injectable ()
export class HeroDataService { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
hero-data.service.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Injectable ()
export class CreditService { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
credit.service.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Injectable ()
export class Logger { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
logger.service.ts
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 02-05}Bootstrapping
2017-02-22 13:09:39 -05:00
#### <a href="#02-05">Style 02-05</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** put bootstrapping and platform logic for the app in a file named `main.ts` .
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** include error handling in the bootstrapping logic.
~~~
~~~ {.s-rule.avoid}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Avoid** putting app logic in `main.ts` . Instead, consider placing it in a component or service.
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Follows a consistent convention for the startup logic of an app.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** Follows a familiar convention from other technology platforms.
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/02-05/main.ts" title = "main.ts" >
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
< a href = "#toc" > Back to top< / a >
### {@a 02-06}Directive selectors
#### <a href="#02-06">Style 02-06</a>
~~~ {.s-rule.do}
**Do** Use lower camel case for naming the selectors of directives.
~~~
~~~ {.s-why}
**Why?** Keeps the names of the properties defined in the directives that are bound to the view consistent with the attribute names.
~~~
~~~ {.s-why-last}
**Why?** The Angular HTML parser is case sensitive and recognizes lower camel case.
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 02-07}Custom prefix for components
2017-02-22 13:09:39 -05:00
#### <a href="#02-07">Style 02-07</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use a hyphenated, lowercase element selector value (e.g. `admin-users` ).
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**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.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use a prefix that identifies the feature area or the app itself.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Prevents element name collisions with components in other apps and with native HTML elements.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** Makes it easier to promote and share the component in other apps.
~~~
~~~ {.s-why-last}
**Why?** Components are easy to identify in the DOM.
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/02-07/app/heroes/hero.component.avoid.ts" region = "example" title = "app/heroes/hero.component.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/02-07/app/users/users.component.avoid.ts" region = "example" title = "app/users/users.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/02-07/app/heroes/hero.component.ts" region = "example" title = "app/heroes/hero.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/02-07/app/users/users.component.ts" region = "example" title = "app/users/users.component.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
### {@a 02-08}Custom prefix for directives
2017-02-22 13:09:39 -05:00
#### <a href="#02-08">Style 02-08</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05: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}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** spell non-element selectors in lower camel case unless the selector is meant to match a native HTML attribute.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Prevents name collisions.
~~~
~~~ {.s-why-last}
**Why?** Directives are easily identified.
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/02-08/app/shared/validate.directive.avoid.ts" region = "example" title = "app/shared/validate.directive.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/02-08/app/shared/validate.directive.ts" region = "example" title = "app/shared/validate.directive.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 02-09}Pipe names
2017-02-22 13:09:39 -05:00
#### <a href="#02-09">Style 02-09</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use consistent names for all pipes, named after their feature.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**Why?** Provides a consistent way to quickly identify and reference pipes.
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
Symbol Name
< / th >
< th >
File Name
< / th >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Pipe ({ name: 'ellipsis' })
export class EllipsisPipe implements PipeTransform { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
ellipsis.pipe.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@Pipe ({ name: 'initCaps' })
export class InitCapsPipe implements PipeTransform { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
init-caps.pipe.ts
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 02-10}Unit test file names
2017-02-22 13:09:39 -05:00
#### <a href="#02-10">Style 02-10</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** name test specification files the same as the component they test.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** name test specification files with a suffix of `.spec` .
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Provides a consistent way to quickly identify tests.
~~~
~~~ {.s-why-last}
**Why?** Provides pattern matching for [karma ](http://karma-runner.github.io/ ) or other test runners.
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
Symbol Name
< / th >
< th >
File Name
< / th >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Components
< / td >
< td >
2017-03-31 19:57:13 -04:00
heroes.component.spec.ts
hero-list.component.spec.ts
hero-detail.component.spec.ts
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Services
< / td >
< td >
2017-03-31 19:57:13 -04:00
logger.service.spec.ts
hero.service.spec.ts
filter-text.service.spec.ts
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Pipes
< / td >
< td >
2017-03-31 19:57:13 -04:00
ellipsis.pipe.spec.ts
init-caps.pipe.spec.ts
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 02-11}_End-to-End_ (E2E) test file names
2017-02-22 13:09:39 -05:00
#### <a href="#02-11">Style 02-11</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** name end-to-end test specification files after the feature they test with a suffix of `.e2e-spec` .
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** Provides a consistent way to quickly identify end-to-end tests.
~~~
~~~ {.s-why-last}
**Why?** Provides pattern matching for test runners and build automation.
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
Symbol Name
< / th >
< th >
File Name
< / th >
< / tr >
< tr style = top >
< td >
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
End-to-End Tests
2017-02-22 13:09:39 -05:00
< / td >
< td >
2017-03-31 19:57:13 -04:00
app.e2e-spec.ts
heroes.e2e-spec.ts
2017-02-22 13:09:39 -05:00
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 02-12}Angular _NgModule_ names
2017-02-22 13:09:39 -05:00
#### <a href="#02-12">Style 02-12</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** append the symbol name with the suffix `Module` .
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** give the file name the `.module.ts` extension.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** name the module after the feature and folder it resides in.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Provides a consistent way to quickly identify and reference modules.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** Upper camel case is conventional for identifying objects that can be instantiated using a constructor.
~~~
~~~ {.s-why-last}
**Why?** Easily identifies the module as the root of the same named feature.
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** suffix a _RoutingModule_ class name with `RoutingModule` .
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** end the filename of a _RoutingModule_ with `-routing.module.ts` .
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**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.
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< table width = "100%" >
< col width = "50%" >
< / col >
< col width = "50%" >
< / col >
< tr >
< th >
Symbol Name
< / th >
< th >
File Name
< / th >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@NgModule ({ ... })
export class AppModule { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
app.module.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@NgModule ({ ... })
export class HeroesModule { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
heroes.module.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@NgModule ({ ... })
export class VillainsModule { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
villains.module.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@NgModule ({ ... })
export class AppRoutingModule { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
app-routing.module.ts
< / td >
< / tr >
< tr style = top >
< td >
< code-example >
2017-03-31 19:57:13 -04:00
@NgModule ({ ... })
export class HeroesRoutingModule { }
2017-02-22 13:09:39 -05:00
< / code-example >
< / td >
< td >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
heroes-routing.module.ts
< / td >
< / tr >
< / table >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Coding conventions
2017-03-27 11:08:53 -04:00
Have a consistent set of coding, naming, and whitespace conventions.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
### {@a 03-01}Classes
2017-02-22 13:09:39 -05:00
#### <a href="#03-01">Style 03-01</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use upper camel case when naming classes.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Follows conventional thinking for class names.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Classes can be instantiated and construct an instance.
By convention, upper camel case indicates a constructable asset.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
< code-example path = "style-guide/src/03-01/app/core/exception.service.avoid.ts" region = "example" title = "app/shared/exception.service.ts" >
< / code-example >
< code-example path = "style-guide/src/03-01/app/core/exception.service.ts" region = "example" title = "app/shared/exception.service.ts" >
< / code-example >
< a href = "#toc" > Back to top< / a >
### {@a 03-02}Constants
#### <a href="#03-02">Style 03-02</a>
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** declare variables with `const` if their values should not change during the application lifetime.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Conveys to readers that the value is invariant.
~~~
~~~ {.s-why-last}
**Why?** TypeScript helps enforce that intent by requiring immediate initialization and by
preventing subsequent re-assignment.
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** spelling `const` variables in lower camel case.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Lower camel case variable names (`heroRoutes`) are easier to read and understand
than the traditional UPPER_SNAKE_CASE names (`HERO_ROUTES`).
~~~
~~~ {.s-why-last}
**Why?** The tradition of naming constants in UPPER_SNAKE_CASE reflects
an era before the modern IDEs that quickly reveal the `const` declaration.
TypeScript prevents accidental reassignment.
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** tolerate _existing_ `const` variables that are spelled in UPPER_SNAKE_CASE.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**Why?** The tradition of UPPER_SNAKE_CASE remains popular and pervasive,
especially in third party modules.
It is rarely worth the effort to change them at the risk of breaking existing code and documentation.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/03-02/app/core/data.service.ts" title = "app/shared/data.service.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 03-03}Interfaces
2017-02-22 13:09:39 -05:00
#### <a href="#03-03">Style 03-03</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** name an interface using upper camel case.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** naming an interface without an `I` prefix.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** using a class instead of an interface.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** < a href = "https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines" target = "_blank" > TypeScript guidelines< / a >
discourage the `I` prefix.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** A class alone is less code than a _class-plus-interface_ .
~~~
~~~ {.s-why}
**Why?** A class can act as an interface (use `implements` instead of `extends` ).
~~~
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** An interface-class can be a provider lookup token in Angular dependency injection.
~~~
< code-example path = "style-guide/src/03-03/app/core/hero-collector.service.avoid.ts" region = "example" title = "app/shared/hero-collector.service.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/03-03/app/core/hero-collector.service.ts" region = "example" title = "app/shared/hero-collector.service.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 03-04}Properties and methods
2017-02-22 13:09:39 -05:00
#### <a href="#03-04">Style 03-04</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use lower camel case to name properties and methods.
~~~
~~~ {.s-rule.avoid}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Avoid** prefixing private properties and methods with an underscore.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Follows conventional thinking for properties and methods.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** JavaScript lacks a true private property or method.
~~~
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** TypeScript tooling makes it easy to identify private vs. public properties and methods.
~~~
< code-example path = "style-guide/src/03-04/app/core/toast.service.avoid.ts" region = "example" title = "app/shared/toast.service.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/03-04/app/core/toast.service.ts" region = "example" title = "app/shared/toast.service.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 03-06}Import line spacing
2017-02-22 13:09:39 -05:00
#### <a href="#03-06">Style 03-06</a>
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** leaving one empty line between third party imports and application imports.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** listing import lines alphabetized by the module.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** listing destructured imported symbols alphabetically.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** The empty line separates _your_ stuff from _their_ stuff.
~~~
~~~ {.s-why-last}
**Why?** Alphabetizing makes it easier to read and locate symbols.
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/03-06/app/heroes/shared/hero.service.avoid.ts" region = "example" title = "app/heroes/shared/hero.service.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/03-06/app/heroes/shared/hero.service.ts" region = "example" title = "app/heroes/shared/hero.service.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Application structure and Angular modules
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.
All of the app's code goes in a folder named `src` .
All feature areas are in their own folder, with their own Angular module.
All content is one asset per file. Each component, service, and pipe is in its own file.
All third party vendor scripts are stored in another folder and not in the `src` folder.
You didn't write them and you don't want them cluttering `src` .
Use the naming conventions for files in this guide.
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-01}_LIFT_
2017-02-22 13:09:39 -05:00
#### <a href="#04-01">Style 04-01</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -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.
2017-02-22 13:09:39 -05:00
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** define the structure to follow these four basic guidelines, listed in order of importance.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**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_?
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-02}Locate
2017-02-22 13:09:39 -05:00
#### <a href="#04-02">Style 04-02</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** make locating code intuitive, simple and fast.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**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.
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-03}Identify
2017-02-22 13:09:39 -05:00
#### <a href="#04-03">Style 04-03</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** name the file such that you instantly know what it contains and represents.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** be descriptive with file names and keep the contents of the file to exactly one component.
~~~
~~~ {.s-rule.avoid}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Avoid** files with multiple components, multiple services, or a mixture.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**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.
~~~
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
~~~ {.l-sub-section}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05: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.
2017-03-27 11:08:53 -04:00
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-04}Flat
2017-02-22 13:09:39 -05:00
#### <a href="#04-04">Style 04-04</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** keep a flat folder structure as long as possible.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** creating sub-folders when a folder reaches seven or more files.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** configuring the IDE to hide distracting, irrelevant files such as generated `.js` and `.js.map` files.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**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.
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-05}_T-DRY_ (Try to be _DRY_)
2017-02-22 13:09:39 -05:00
#### <a href="#04-05">Style 04-05</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** be DRY (Don't Repeat Yourself).
2017-02-22 13:09:39 -05:00
~~~
~~~ {.s-rule.avoid}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Avoid** being so DRY that you sacrifice readability.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**Why?** Being DRY is important, but not crucial if it sacrifices the other elements of LIFT.
That's why it's called _T-DRY_ .
For example, it's redundant to name a template `hero-view.component.html` because
with the `.html` extension, it is obviously a view.
But if something is not obvious or departs from a convention, then spell it out.
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-06}Overall structural guidelines
2017-02-22 13:09:39 -05:00
#### <a href="#04-06">Style 04-06</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** start small but keep in mind where the app is heading down the road.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** have a near term view of implementation and a long term vision.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** put all of the app's code in a folder named `src` .
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** creating a folder for a component when it has multiple accompanying files (`.ts`, `.html` , `.css` and `.spec` ).
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -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.
~~~
~~~ {.s-why-last}
**Why?** Components often have four files (e.g. `*.html` , `*.css` , `*.ts` , and `*.spec.ts` ) and can clutter a folder quickly.
~~~
2017-02-22 13:09:39 -05:00
{@a file-tree}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Here is a compliant folder and file structure:
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< aio-filetree >
< aio-folder >
< project root>
< aio-folder >
src
< aio-folder >
app
< aio-folder >
core
< aio-file >
core.module.ts
< / aio-file >
< aio-file >
exception.service.ts|spec.ts
< / aio-file >
< aio-file >
user-profile.service.ts|spec.ts
< / aio-file >
< / aio-folder >
< aio-folder >
heroes
< aio-folder >
hero
< aio-file >
hero.component.ts|html|css|spec.ts
< / aio-file >
< / aio-folder >
< aio-folder >
hero-list
< aio-file >
hero-list.component.ts|html|css|spec.ts
< / aio-file >
< / aio-folder >
< aio-folder >
shared
< aio-file >
hero-button.component.ts|html|css|spec.ts
< / aio-file >
< aio-file >
hero.model.ts
< / aio-file >
< aio-file >
hero.service.ts|spec.ts
< / aio-file >
< / aio-folder >
< aio-file >
heroes.component.ts|html|css|spec.ts
< / aio-file >
< aio-file >
heroes.module.ts
< / aio-file >
< aio-file >
heroes-routing.module.ts
< / aio-file >
< / aio-folder >
< aio-folder >
shared
< aio-file >
shared.module.ts
< / aio-file >
< aio-file >
init-caps.pipe.ts|spec.ts
< / aio-file >
< aio-file >
text-filter.component.ts|spec.ts
< / aio-file >
< aio-file >
text-filter.service.ts|spec.ts
< / aio-file >
< / aio-folder >
< aio-folder >
villains
< aio-folder >
villain
< aio-file >
...
< / aio-file >
< / aio-folder >
< aio-folder >
villain-list
< aio-file >
...
< / aio-file >
< / aio-folder >
< aio-folder >
shared
< aio-file >
...
< / aio-file >
< / aio-folder >
< aio-file >
villains.component.ts|html|css|spec.ts
< / aio-file >
< aio-file >
villains.module.ts
< / aio-file >
< aio-file >
villains-routing.module.ts
< / aio-file >
< / aio-folder >
< aio-file >
app.component.ts|html|css|spec.ts
< / aio-file >
< aio-file >
app.module.ts
< / aio-file >
< aio-file >
app-routing.module.ts
< / aio-file >
< / aio-folder >
< aio-file >
main.ts
< / aio-file >
< aio-file >
index.html
< / aio-file >
< aio-file >
...
< / aio-file >
< / aio-folder >
< aio-file >
node_modules/...
< / aio-file >
< aio-file >
...
< / aio-file >
< / aio-folder >
< / aio-filetree >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
~~~ {.l-sub-section}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
While components in dedicated folders 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.
2017-03-27 11:08:53 -04:00
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-07}_Folders-by-feature_ structure
2017-02-22 13:09:39 -05:00
#### <a href="#04-07">Style 04-07</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** create folders named for the feature area they represent.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** A developer can locate the code and identify what each file represents
at a glance. The structure is as flat as it can be and there are no repetitive or redundant names.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** The LIFT guidelines are all covered.
~~~
~~~ {.s-why}
**Why?** Helps reduce the app from becoming cluttered through organizing the
content and keeping them aligned with the LIFT guidelines.
~~~
~~~ {.s-why}
**Why?** When there are a lot of files, for example 10+,
locating them is easier with a consistent folder structure
and more difficult in a flat structure.
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** create an Angular module for each feature area.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Angular modules make it easy to lazy load routable features.
~~~
~~~ {.s-why-last}
**Why?** Angular modules make it easier to isolate, test, and re-use features.
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< div class = 'file-tree-reference' >
2017-03-31 19:57:13 -04:00
< a href = "#file-tree" > Refer to this _folder and file structure_ example.</ a >
2017-02-22 13:09:39 -05:00
< / div >
2017-03-31 19:57:13 -04:00
< a href = "#toc" > Back to top
< / a >
### {@a 04-08}App _root module_
2017-02-22 13:09:39 -05:00
#### <a href="#04-08">Style 04-08</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** create an Angular module in the app's root folder,
for example, in `/src/app` .
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** Every app requires at least one root Angular module.
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** naming the root module `app.module.ts` .
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**Why?** Makes it easier to locate and identify the root module.
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/04-08/app/app.module.ts" region = "example" title = "app/app.module.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-09}Feature modules
2017-02-22 13:09:39 -05:00
#### <a href="#04-09">Style 04-09</a>
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** create an Angular module for all distinct features in an application;
for example, a `Heroes` feature.
2017-02-22 13:09:39 -05:00
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** place the feature module in the same named folder as the feature area;
for example, in `app/heroes` .
2017-02-22 13:09:39 -05:00
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** name the feature module file reflecting the name of the feature area
and folder; for example, `app/heroes/heroes.module.ts` .
2017-02-22 13:09:39 -05:00
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** name the feature module symbol reflecting the name of the feature
area, folder, and file; for example, `app/heroes/heroes.module.ts` defines `HeroesModule` .
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** A feature module can expose or hide its implementation from other modules.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** A feature module identifies distinct sets of related components that comprise the feature area.
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** A feature module can easily be routed to both eagerly and lazily.
~~~
~~~ {.s-why}
**Why?** A feature module defines clear boundaries between specific functionality and other application features.
~~~
~~~ {.s-why}
**Why?** A feature module helps clarify and make it easier to assign development responsibilities to different teams.
~~~
~~~ {.s-why-last}
**Why?** A feature module can easily be isolated for testing.
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-10}Shared feature module
2017-02-22 13:09:39 -05:00
#### <a href="#04-10">Style 04-10</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** create a feature module named `SharedModule` in a `shared` folder;
for example, `app/shared/shared.module.ts` defines `SharedModule` .
2017-02-22 13:09:39 -05:00
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** declare components, directives, and pipes in a shared module when those
items will be re-used and referenced by the components declared in other feature modules.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Consider** using the name SharedModule when the contents of a shared
2017-02-22 13:09:39 -05:00
module are referenced across the entire application.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** not provide services in shared modules. Services are usually
singletons that are provided once for the entire application or
in a particular feature module.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** import all modules required by the assets in the `SharedModule` ;
for example, `CommonModule` and `FormsModule` .
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** `SharedModule` will contain components, directives and pipes
that may need features from another common module; for example,
`ngFor` in `CommonModule` .
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** declare all components, directives, and pipes in the `SharedModule` .
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** export all symbols from the `SharedModule` that other feature modules need to use.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**Why?** `SharedModule` exists to make commonly used components, directives and pipes available for use in the templates of components in many other modules.
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.avoid}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Avoid** specifying app-wide singleton providers in a `SharedModule` . Intentional singletons are OK. Take care.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -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.
~~~
~~~ {.s-why-last}
**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.
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
< aio-filetree >
< aio-folder >
src
< aio-folder >
app
< aio-folder >
shared
< aio-file >
shared.module.ts
< / aio-file >
< aio-file >
init-caps.pipe.ts|spec.ts
< / aio-file >
< aio-file >
text-filter.component.ts|spec.ts
< / aio-file >
< aio-file >
text-filter.service.ts|spec.ts
< / aio-file >
< / aio-folder >
< aio-file >
app.component.ts|html|css|spec.ts
< / aio-file >
< aio-file >
app.module.ts
< / aio-file >
< aio-file >
app-routing.module.ts
< / aio-file >
< / aio-folder >
< aio-file >
main.ts
< / aio-file >
< aio-file >
index.html
< / aio-file >
< / aio-folder >
< aio-file >
...
< / aio-file >
< / aio-filetree >
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
< code-tabs >
< code-pane title = "app/shared/shared.module.ts" path = "style-guide/src/04-10/app/shared/shared.module.ts" >
< / code-pane >
< code-pane title = "app/shared/init-caps.pipe.ts" path = "style-guide/src/04-10/app/shared/init-caps.pipe.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/shared/filter-text/filter-text.component.ts" path = "style-guide/src/04-10/app/shared/filter-text/filter-text.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/shared/filter-text/filter-text.service.ts" path = "style-guide/src/04-10/app/shared/filter-text/filter-text.service.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/heroes/heroes.component.ts" path = "style-guide/src/04-10/app/heroes/heroes.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/heroes/heroes.component.html" path = "style-guide/src/04-10/app/heroes/heroes.component.html" >
< / code-pane >
< / code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-11}Core feature module
2017-02-22 13:09:39 -05:00
#### <a href="#04-11">Style 04-11</a>
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** collecting numerous, auxiliary, single-use classes inside a core module
to simplify the apparent structure of a feature module.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** calling the application-wide core module, `CoreModule` .
Importing `CoreModule` into the root `AppModule` reduces its complexity
and emphasizes its role as orchestrator of the application as a whole.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** create a feature module named `CoreModule` in a `core` folder (e.g. `app/core/core.module.ts` defines `CoreModule` ).
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** put a singleton service whose instance will be shared throughout the application in the `CoreModule` (e.g. `ExceptionService` and `LoggerService` ).
2017-02-22 13:09:39 -05:00
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** import all modules required by the assets in the `CoreModule` (e.g. `CommonModule` and `FormsModule` ).
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -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.
~~~
~~~ {.s-why}
**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.
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**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` ).
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
**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.
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.avoid}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Avoid** importing the `CoreModule` anywhere except in the `AppModule` .
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** A lazily loaded feature module that directly imports the `CoreModule` will make its own copy of services and likely have undesireable results.
~~~
~~~ {.s-why}
**Why?** An eagerly loaded feature module already has access to the `AppModule` 's injector, and thus the `CoreModule` 's services.
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** export all symbols from the `CoreModule` that the `AppModule` will import and make available for other feature modules to use.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** `CoreModule` exists to make commonly used singleton services available for use in the many other modules.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**Why?** You want 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.
~~~
< aio-filetree >
< aio-folder >
2017-02-22 13:09:39 -05:00
src
< aio-folder >
app
< aio-folder >
core
< aio-file >
core.module.ts
< / aio-file >
< aio-file >
logger.service.ts|spec.ts
< / aio-file >
< aio-folder >
nav
< aio-file >
nav.component.ts|html|css|spec.ts
< / aio-file >
< / aio-folder >
< aio-folder >
spinner
< aio-file >
spinner.component.ts|html|css|spec.ts
< / aio-file >
< aio-file >
spinner.service.ts|spec.ts
< / aio-file >
< / aio-folder >
< / aio-folder >
< aio-file >
app.component.ts|html|css|spec.ts
< / aio-file >
< aio-file >
app.module.ts
< / aio-file >
< aio-file >
app-routing.module.ts
< / aio-file >
< / aio-folder >
< aio-file >
main.ts
< / aio-file >
< aio-file >
index.html
< / aio-file >
< / aio-folder >
< aio-file >
...
< / aio-file >
< / aio-filetree >
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
< code-tabs >
< code-pane title = "app/app.module.ts" path = "style-guide/src/04-11/app/app.module.ts" region = "example" >
< / code-pane >
< code-pane title = "app/core/core.module.ts" path = "style-guide/src/04-11/app/core/core.module.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/core/logger.service.ts" path = "style-guide/src/04-11/app/core/logger.service.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/core/nav/nav.component.ts" path = "style-guide/src/04-11/app/core/nav/nav.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/core/nav/nav.component.html" path = "style-guide/src/04-11/app/core/nav/nav.component.html" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/core/spinner/spinner.component.ts" path = "style-guide/src/04-11/app/core/spinner/spinner.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/core/spinner/spinner.component.html" path = "style-guide/src/04-11/app/core/spinner/spinner.component.html" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/core/spinner/spinner.service.ts" path = "style-guide/src/04-11/app/core/spinner/spinner.service.ts" >
< / code-pane >
< / code-tabs >
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
~~~ {.l-sub-section}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
`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.
2017-03-27 11:08:53 -04:00
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-12}Prevent re-import of the core module
2017-02-22 13:09:39 -05:00
#### <a href="#04-12">Style 04-12</a>
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
Only the root `AppModule` should import the `CoreModule` .
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** guard against reimporting of `CoreModule` and fail fast by adding guard logic.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Guards against reimporting of the `CoreModule` .
~~~
~~~ {.s-why-last}
**Why?** Guards against creating multiple instances of assets intended to be singletons.
~~~
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-tabs >
< code-pane title = "app/core/module-import-guard.ts" path = "style-guide/src/04-12/app/core/module-import-guard.ts" >
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/core/core.module.ts" path = "style-guide/src/04-12/app/core/core.module.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-13}Lazy Loaded folders
2017-02-22 13:09:39 -05:00
#### <a href="#04-13">Style 04-13</a>
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
A distinct application feature or workflow may be *lazy loaded* or *loaded on demand* rather than when the application starts.
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05: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.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**Why?** The folder makes it easy to identify and isolate the feature content.
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 04-14}Never directly import lazy loaded folders
2017-02-22 13:09:39 -05:00
#### <a href="#04-14">Style 04-14</a>
~~~ {.s-rule.avoid}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Avoid** allowing modules in sibling and parent folders to directly import a module in a *lazy loaded feature* .
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**Why?** Directly importing and using a module will load it immediately when the intention is to load it on demand.
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Components
2017-03-31 19:57:13 -04:00
### {@a 05-02}Component selector names
2017-02-22 13:09:39 -05:00
#### <a href="#05-02">Style 05-02</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use _dashed-case_ or _kebab-case_ for naming the element selectors of components.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
**Why?** Keeps the element names consistent with the specification for [Custom Elements ](https://www.w3.org/TR/custom-elements/ ).
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/05-02/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region = "example" title = "app/heroes/shared/hero-button/hero-button.component.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
< code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/heroes/shared/hero-button/hero-button.component.ts" path = "style-guide/src/05-02/app/heroes/shared/hero-button/hero-button.component.ts" region = "example" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/app.component.html" path = "style-guide/src/05-02/app/app.component.html" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 05-03}Components as elements
2017-02-22 13:09:39 -05:00
#### <a href="#05-03">Style 05-03</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** give components an _element_ selector, as opposed to _attribute_ or _class_ selectors.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** components have templates containing HTML and optional Angular template syntax.
They display content.
Developers place components on the page as they would native HTML elements and web components.
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** It is easier to recognize that a symbol is a component by looking at the template's html.
~~~
< code-example path = "style-guide/src/05-03/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region = "example" title = "app/heroes/hero-button/hero-button.component.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/05-03/app/app.component.avoid.html" title = "app/app.component.html" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
< code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/heroes/shared/hero-button/hero-button.component.ts" path = "style-guide/src/05-03/app/heroes/shared/hero-button/hero-button.component.ts" region = "example" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/app.component.html" path = "style-guide/src/05-03/app/app.component.html" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
< / code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 05-04}Extract templates and styles to their own files
2017-02-22 13:09:39 -05:00
#### <a href="#05-04">Style 05-04</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** extract templates and styles into a separate file, when more than 3 lines.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** name the template file `[component-name].component.html` , where [component-name] is the component name.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** name the style file `[component-name].component.css` , where [component-name] is the component name.
~~~
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** specify _component-relative_ URLs, prefixed with `./` .
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Large, inline templates and styles obscure the component's purpose and implementation, reducing readability and maintainability.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** In most editors, syntax hints and code snippets aren't available when developing inline templates and styles.
The Angular TypeScript Language Service (forthcoming) promises to overcome this deficiency for HTML templates
in those editors that support it; it won't help with CSS styles.
~~~
~~~ {.s-why}
**Why?** A _component relative_ URL requires no change when you move the component files, as long as the files stay together.
~~~
~~~ {.s-why-last}
**Why?** The `./` prefix is standard syntax for relative URLs; don't depend on Angular's current ability to do without that prefix.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
< code-example path = "style-guide/src/05-04/app/heroes/heroes.component.avoid.ts" region = "example" title = "app/heroes/heroes.component.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
< code-tabs >
< code-pane title = "app/heroes/heroes.component.ts" path = "style-guide/src/05-04/app/heroes/heroes.component.ts" region = "example" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/heroes/heroes.component.html" path = "style-guide/src/05-04/app/heroes/heroes.component.html" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< code-pane title = "app/heroes/heroes.component.css" path = "style-guide/src/05-04/app/heroes/heroes.component.css" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 05-12}Decorate _input_ and _output_ properties
2017-02-22 13:09:39 -05:00
#### <a href="#05-12">Style 05-12</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
**Do** use the `@Input()` and `@Output()` class decorators instead of the `inputs` and `outputs` properties of the
2017-02-22 13:09:39 -05:00
`@Directive` and `@Component` metadata:
~~~
2017-03-31 19:57:13 -04:00
~~~ {.s-rule.do}
**Consider** placing `@Input()` or `@Output()` on the same line as the property it decorates.
~~~
~~~ {.s-why}
**Why?** It is easier and more readable to identify which properties in a class are inputs or outputs.
~~~
~~~ {.s-why}
**Why?** If you ever need to rename the property or event name associated with
`@Input` or `@Output` , you can modify it in a single place.
~~~
~~~ {.s-why}
**Why?** The metadata declaration attached to the directive is shorter and thus more readable.
~~~
~~~ {.s-why-last}
**Why?** Placing the decorator on the same line _usually_ makes for shorter code and still easily identifies the property as an input or output.
Put it on the line above when doing so is clearly more readable.
~~~
< code-example path = "style-guide/src/05-12/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region = "example" title = "app/heroes/shared/hero-button/hero-button.component.ts" >
< / code-example >
< code-example path = "style-guide/src/05-12/app/heroes/shared/hero-button/hero-button.component.ts" region = "example" title = "app/heroes/shared/hero-button/hero-button.component.ts" >
< / code-example >
< a href = "#toc" > Back to top< / a >
### {@a 05-13}Avoid aliasing _inputs_ and _outputs_
#### <a href="#05-13">Style 05-13</a>
~~~ {.s-rule.avoid}
**Avoid** _input_ and _output_ aliases except when it serves an important purpose.
~~~
~~~ {.s-why}
**Why?** Two names for the same property (one private, one public) is inherently confusing.
~~~
~~~ {.s-why-last}
**Why?** You should use an alias when the directive name is also an _input_ property,
and the directive name doesn't describe the property.
~~~
< code-example path = "style-guide/src/05-13/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region = "example" title = "app/heroes/shared/hero-button/hero-button.component.ts" >
< / code-example >
< code-example path = "style-guide/src/05-13/app/app.component.avoid.html" title = "app/app.component.html" >
< / code-example >
< code-tabs >
< code-pane title = "app/heroes/shared/hero-button/hero-button.component.ts" path = "style-guide/src/05-13/app/heroes/shared/hero-button/hero-button.component.ts" region = "example" >
< / code-pane >
< code-pane title = "app/heroes/shared/hero-button/hero-highlight.directive.ts" path = "style-guide/src/05-13/app/heroes/shared/hero-highlight.directive.ts" >
< / code-pane >
< code-pane title = "app/app.component.html" path = "style-guide/src/05-13/app/app.component.html" >
< / code-pane >
< / code-tabs >
< a href = "#toc" > Back to top< / a >
### {@a 05-14}Member sequence
#### <a href="#05-14">Style 05-14</a>
~~~ {.s-rule.do}
**Do** place properties up top followed by methods.
~~~
~~~ {.s-rule.do}
**Do** place private members after public members, alphabetized.
~~~
~~~ {.s-why-last}
**Why?** Placing members in a consistent sequence makes it easy to read and
helps instantly identify which members of the component serve which purpose.
~~~
< code-example path = "style-guide/src/05-14/app/shared/toast/toast.component.avoid.ts" region = "example" title = "app/shared/toast/toast.component.ts" >
< / code-example >
< code-example path = "style-guide/src/05-14/app/shared/toast/toast.component.ts" region = "example" title = "app/shared/toast/toast.component.ts" >
< / code-example >
< a href = "#toc" > Back to top< / a >
### {@a 05-15}Delegate complex component logic to services
#### <a href="#05-15">Style 05-15</a>
~~~ {.s-rule.do}
**Do** limit logic in a component to only that required for the view. All other logic should be delegated to services.
~~~
~~~ {.s-rule.do}
**Do** move reusable logic to services and keep components simple and focused on their intended purpose.
~~~
~~~ {.s-why}
**Why?** Logic may be reused by multiple components when placed within a service and exposed via a function.
~~~
~~~ {.s-why}
**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.
~~~
~~~ {.s-why}
**Why?** Removes dependencies and hides implementation details from the component.
~~~
~~~ {.s-why-last}
**Why?** Keeps the component slim, trim, and focused.
~~~
< code-example path = "style-guide/src/05-15/app/heroes/hero-list/hero-list.component.avoid.ts" title = "app/heroes/hero-list/hero-list.component.ts" >
< / code-example >
< code-example path = "style-guide/src/05-15/app/heroes/hero-list/hero-list.component.ts" region = "example" title = "app/heroes/hero-list/hero-list.component.ts" >
< / code-example >
< a href = "#toc" > Back to top< / a >
### {@a 05-16}Don't prefix _output_ properties
#### <a href="#05-16">Style 05-16</a>
~~~ {.s-rule.do}
**Do** name events without the prefix `on` .
~~~
~~~ {.s-rule.do}
**Do** name event handler methods with the prefix `on` followed by the event name.
~~~
~~~ {.s-why}
**Why?** This is consistent with built-in events such as button clicks.
~~~
~~~ {.s-why-last}
**Why?** Angular allows for an [alternative syntax ](guide/template-syntax#binding-syntax ) `on-*` . If the event itself was prefixed with `on` this would result in an `on-onEvent` binding expression.
~~~
< code-example path = "style-guide/src/05-16/app/heroes/hero.component.avoid.ts" region = "example" title = "app/heroes/hero.component.ts" >
< / code-example >
< code-example path = "style-guide/src/05-16/app/app.component.avoid.html" title = "app/app.component.html" >
< / code-example >
< code-tabs >
< code-pane title = "app/heroes/hero.component.ts" path = "style-guide/src/05-16/app/heroes/hero.component.ts" region = "example" >
< / code-pane >
< code-pane title = "app/app.component.html" path = "style-guide/src/05-16/app/app.component.html" >
< / code-pane >
< / code-tabs >
< a href = "#toc" > Back to top< / a >
### {@a 05-17}Put presentation logic in the component class
#### <a href="#05-17">Style 05-17</a>
~~~ {.s-rule.do}
**Do** put presentation logic in the component class, and not in the template.
~~~
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** Logic will be contained in one place (the component class) instead of being spread in two places.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** Keeping the component's presentation logic in the class instead of the template improves testability, maintainability, and reusability.
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/05-17/app/heroes/hero-list/hero-list.component.avoid.ts" region = "example" title = "app/heroes/hero-list/hero-list.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/05-17/app/heroes/hero-list/hero-list.component.ts" region = "example" title = "app/heroes/hero-list/hero-list.component.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
## Directives
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
### {@a 06-01}Use directives to enhance an element
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
#### <a href="#06-01">Style 06-01</a>
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-rule.do}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Do** use attribute directives when you have presentation logic without a template.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** Attribute directives don't have an associated template.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** An element may have more than one attribute directive applied.
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/06-01/app/shared/highlight.directive.ts" region = "example" title = "app/shared/highlight.directive.ts" >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/06-01/app/app.component.html" title = "app/app.component.html" >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< a href = "#toc" > Back to top< / a >
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
### {@a 06-03}_HostListener_/_HostBinding_ decorators versus _host_ metadata
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
#### <a href="#06-03">Style 06-03</a>
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-rule.consider}
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Consider** preferring the `@HostListener` and `@HostBinding` to the
`host` property of the `@Directive` and `@Component` decorators.
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
**Do** be consistent in your choice.
2017-02-22 13:09:39 -05:00
~~~
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -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.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/06-03/app/shared/validator.directive.ts" title = "app/shared/validator.directive.ts" >
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
Compare with the less preferred `host` metadata alternative.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** The `host` metadata is only one term to remember and doesn't require extra ES imports.
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/06-03/app/shared/validator2.directive.ts" title = "app/shared/validator2.directive.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< a href = "#toc" > Back to top< / a >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
## Services
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
### {@a 07-01}Services are singletons
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
#### <a href="#07-01">Style 07-01</a>
2017-02-22 13:09:39 -05:00
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
**Do** use services as singletons within the same injector. Use them for sharing data and functionality.
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Services are ideal for sharing methods across a feature area or an app.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
**Why?** Services are ideal for sharing stateful in-memory data.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/07-01/app/heroes/shared/hero.service.ts" region = "example" title = "app/heroes/shared/hero.service.ts" >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 07-02}Single responsibility
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
#### <a href="#07-02">Style 07-02</a>
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-rule.do}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Do** create services with a single responsibility that is encapsulated by its context.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-rule.do}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Do** create a new service once the service begins to exceed that singular purpose.
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** When a service has multiple responsibilities, it becomes difficult to test.
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** When a service has multiple responsibilities, every component or service that injects it now carries the weight of them all.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< a href = "#toc" > Back to top< / a >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
### {@a 07-03}Providing a service
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
#### <a href="#07-03">Style 07-03</a>
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-rule.do}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Do** provide services to the Angular injector at the top-most component where they will be shared.
2017-02-22 13:09:39 -05:00
~~~
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** The Angular injector is hierarchical.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**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.
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** This is ideal when a service is sharing methods or state.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**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.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-pane title = "app/app.component.ts" path = "style-guide/src/07-03/app/app.component.ts" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-pane title = "app/heroes/hero-list/hero-list.component.ts" path = "style-guide/src/07-03/app/heroes/hero-list/hero-list.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< / code-pane >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< / code-tabs >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< a href = "#toc" > Back to top< / a >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
### {@a 07-04}Use the @Injectable() class decorator
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
#### <a href="#07-04">Style 07-04</a>
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-rule.do}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -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.
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** The Angular Dependency Injection (DI) mechanism resolves a service's own
dependencies based on the declared types of that service's constructor parameters.
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**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.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/07-04/app/heroes/shared/hero-arena.service.avoid.ts" region = "example" title = "app/heroes/shared/hero-arena.service.ts" >
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/07-04/app/heroes/shared/hero-arena.service.ts" region = "example" title = "app/heroes/shared/hero-arena.service.ts" >
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
## Data Services
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
### {@a 08-01}Talk to the server through a service
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
#### <a href="#08-01">Style 08-01</a>
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-rule.do}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Do** refactor logic for making data operations and interacting with data to a service.
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-rule.do}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Do** make data services responsible for XHR calls, local storage, stashing in memory, or any other data operations.
2017-03-27 11:08:53 -04:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**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.
2017-02-22 13:09:39 -05:00
~~~
2017-03-31 19:57:13 -04:00
~~~ {.s-why}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** This makes it easier to test (mock or real) the data calls when testing a component that uses a data service.
2017-02-22 13:09:39 -05:00
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
**Why?** The details of data management, such as headers, HTTP methods,
caching, error handling, and retry logic, are irrelevant to components
and other data consumers.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
A data service encapsulates these details. It's easier to evolve these
details inside the service without affecting its consumers. And it's
easier to test the consumers with mock service implementations.
2017-02-22 13:09:39 -05:00
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Lifecycle hooks
Use Lifecycle hooks to tap into important events exposed by Angular.
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a 09-01}Implement lifecycle hook interfaces
2017-02-22 13:09:39 -05:00
#### <a href="#09-01">Style 09-01</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** implement the lifecycle hook interfaces.
~~~
2017-03-30 15:04:18 -04:00
2017-03-31 19:57:13 -04:00
~~~ {.s-why-last}
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
**Why?** Lifecycle interfaces prescribe typed method
signatures. use those signatures to flag spelling and syntax mistakes.
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
~~~
< code-example path = "style-guide/src/09-01/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region = "example" title = "app/heroes/shared/hero-button/hero-button.component.ts" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
< code-example path = "style-guide/src/09-01/app/heroes/shared/hero-button/hero-button.component.ts" region = "example" title = "app/heroes/shared/hero-button/hero-button.component.ts" >
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
## Appendix
Useful tools and tips for Angular.
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a A-01}Codelyzer
2017-02-22 13:09:39 -05:00
#### <a href="#A-01">Style A-01</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Do** use [codelyzer ](https://www.npmjs.com/package/codelyzer ) to follow this guide.
~~~
~~~ {.s-rule.consider}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**Consider** adjusting the rules in codelyzer to suit your needs.
~~~
< a href = "#toc" > Back to top< / a >
2017-03-31 19:57:13 -04:00
### {@a A-02}File templates and snippets
2017-02-22 13:09:39 -05:00
#### <a href="#A-02">Style A-02</a>
~~~ {.s-rule.do}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
**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}
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05: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.
< a href = "https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2" >
< img src = "https://github.com/johnpapa/vscode-angular2-snippets/raw/master/images/use-extension.gif" width = "80%" alt = "Use Extension" >
< / a >
**Consider** using [snippets ](https://atom.io/packages/angular-2-typescript-snippets ) for [Atom ](https://atom.io/ ) that follow these styles and guidelines.
**Consider** using [snippets ](https://github.com/orizens/sublime-angular2-snippets ) for [Sublime Text ](http://www.sublimetext.com/ ) that follow these styles and guidelines.
**Consider** using [snippets ](https://github.com/mhartington/vim-angular2-snippets ) for [Vim ](http://www.vim.org/ ) that follow these styles and guidelines.
~~~
< a href = "#toc" > Back to top< / a >