- App Modules are registered in
app/index.js.
Routes are pathways within the application.
- Route modules are registered in
app/route.js. - The structure of a route path looks like the:
{{component}}/{{template}}.html. ui-routerhandles all the routing in Rectangular apps - you can learn more about it here.
A route looks something like this:
angular
.module("home.route", [
"home.component"
])
.config(($stateProvider) => {
$stateProvider
.state("home", {
"url": "/",
"template": "<home></home>"
})
;
})
;
Services are used for sending/receiving/manipulating data.
- You can use services to organize and share code across your app.
Directives are used to control DOM behaviour and to render data in HTML format.
- The structure of a
directivetemplate path looks like the:{{component|directive}}/{{template}}.directive.html. - A
directiveshould decorate, and should generally be implemented as an attribute and restricted to 'A'.
A component is a specialized directive that organizes a controller with a template. One main difference between a component and a directive is that a component doesn't have a link function.
- A
componentis restricted to 'E' by default, meaning custom element. - For DOM decoration, a component can use directives.
- Relevant files are placed in the
app/component/{{componentName}}directory:- Components (js)
- Directives (js)
- Templates (html)
- Routes (js)
- Services (js)
- Tests (js)
- Styles (scss)
- Models ([data|mixin]json)
- Images (bmp, gif, ico, jpeg, jpg, png, svg)
A simple AngularJS component may resemble the following:
const options = {};
options.templateUrl = "home/home.html";
options.controllerAs = "vm";
options.controller = function HomeController() {
const vm = this;
};
angular
.module("home.component", [])
.component("home", options)
;
Controllers are used in both components and directives.
-
Controllers should be used for:
- Setting up the initial state of the View Model.
- Adding behavior to the View Model.
-
Do not use controllers:
- For anything other than business logic.
- To share code or state across controllers — use services instead.
- A short capture variable called
vmis generally used for theView Modelobject. - The
thiskeyword is contextual and when used within a function inside a controller it may change it's context. - Capturing the context of this with
vmavoids encountering this problem. - The
vmvariable is assigned using thecontrollerAssyntax.
- The structure of a template path looks like the:
{{component}}/{{template}}.html.
<ng-include src="'{{component}}/{{template}}.html'"></ng-include>
- The ngAnimate module provides support for CSS-based and JavaScript-based animations.
- The ngMessage module commonly is to display error messages for inputs.
- The ngSanitize module provides functionality to sanitize HTML.
- The ngAria module provides support for common ARIA attributes used in assistive technologies.