-1
日增长
5,904
stars
677
forks
322
watchers
75
prs
103
issues
0
releases
README
AngularJS styleguide (ES2015)
Up-to-date with AngularJS 1.6 best practices. Architecture, file structure, components, one-way dataflow, lifecycle hooks.
---
Want an example structure as reference? Check out my component based architecture 1.5 app.
---
A sensible styleguide for teams by @toddmotto
This architecture and styleguide has been rewritten from the ground up for ES2015, the changes in AngularJS 1.5+ for future-upgrading your application to Angular. This guide includes new best practices for one-way dataflow, event delegation, component architecture and component routing.
You can find the old styleguide here, and the reasoning behind the new one here.
Join the Ultimate AngularJS learning experience to fully master beginner and advanced AngularJS features to build real-world apps that are fast, and scale.
Table of Contents
1. Modular architecture
1. Theory
1. Root module
1. Component module
1. Common module
1. Low-level modules
1. File naming conventions
1. Scalable file structure
1. Components
1. Theory
1. Supported properties
1. Controllers
1. One-way dataflow and Events
1. Stateful Components
1. Stateless Components
1. Routed Components
1. Directives
1. Theory
1. Recommended properties
1. Constants or Classes
1. Services
1. Theory
1. Classes for Service
1. Styles
1. ES2015 and Tooling
1. State management
1. Resources
1. Documentation
1. Contributing
Modular architecture
Each module in an Angular app is a module component. A module component is the root definition for that module that encapsulates the logic, templates, routing and child components.
Module theory
The design in the modules maps directly to our folder structure, which keeps things maintainable and predictable. We should ideally have three high-level modules: root, component and common. The root module defines the base module that bootstraps our app, and the corresponding template. We then import our component and common modules into the root module to include our dependencies. The component and common modules then require lower-level component modules, which contain our components, controllers, services, directives, filters and tests for each reusable feature.
Back to top
Root module
A root module begins with a root component that defines the base element for the entire application, with a routing outlet defined, example shown using ui-view from ui-router.
js
// app.component.js
export const AppComponent = {
template:
Hello world
Copyright MyApp 2016.
};
A root module is then created, with AppComponent imported and registered with .component('app', AppComponent). Further imports for submodules (component and common modules) are made to include all components relevant for the application. You'll notice styles are also being imported here, we'll come onto this in later chapters in this guide.
js
// app.moduleTop issues
#130Lazy Loading10 comments · 2016-10-19#85Component Controller - Inject new service require change in 3 place7 comments · 2016-06-27#156Parent component trigger function in child component5 comments · 2017-03-28#161ng-annotate4 comments · 2017-05-26#144Extract `class` controller declaration from component declaration4 comments · 2016-12-21#90component communication and triggers4 comments · 2016-07-13#146Yeoman generator for this style guide3 comments · 2017-01-13#175Can't access form object in component's controller1 comments · 2018-11-01