...
App Structure
- Stick to the one file equals one AngularJS module principle. This will allow you to maintain relatively small, focused files and modules. Additionally you won't be concerned with the load order of those files. Also it will be possible to load individual modules under unit tests.
Spreading route definitions among several modules
In a large scale web application, it is not uncommon to have dozens and dozens of different routes. While the $routeProvider service offers a very nice, fluent style API, route definitions can get quite verbose (especially when the resolve property is used). If we combine a large number of routes with the relative verboseness of each definition, we quickly end up maintaining a huge JavaScript file with several hundreds of lines of code! Worse yet, having all routes defined in one huge file means that this file must be modified quite frequently by all developers working on a project - a recipe for creating a bottlenecks and disastrous merge issues.
With AngularJS, we are not forced to define all the routes in one central file! If we take the approach of modularizing each functional area (has its own dedicated module), we can move routes linked to a certain part of an application to the corresponding module.
In the AngularJS module system, each and every module can have its associated config function, where we can inject $routeProvider service and define routes. For example: each of these submodules defines its own routes as follows:
Code Block | ||||
---|---|---|---|---|
| ||||
angular.module('admin-users', [])
.config(function ($routeProvider) {
$routeProvider.when('/admin/users', {...});
$routeProvider.when('/admin/users/new', {...});
$routeProvider.when('/admin/users/:userId', {...});
});
angular.module('admin-projects', [])
.config(function ($routeProvider) {
$routeProvider.when('/admin/users', {...});
$routeProvider.when('/admin/users/new', {...});
$routeProvider.when('/admin/users/:userId', {...});
});
angular.module('admin', ['admin-projects', 'admin-users']);
|
Directives
- While declaring an AngularJS directive, the naming convention followed is camelCase. For example, we would define the name of the directive as 'myDatePicker'. But when you actually use the directive in your HTML, it is the dash-separated version (my-date-picker).
- To make your directives and attributes HTML5 compliant, you can prefix all AngularJS attributes with "data-" or "x-". That is, "x-ng-show" or "data-date-picker", and AngularJS will treat it just like you wrote it like a normal HTML element.
...