Simple AngularJs Routing

Simple generic routing

Module provides generic routing configuration. It works in applications that most of the time use one controller for one page. In that situation it is normal to specify controller in html.

Example

With configuration

angular.module('myApp', ['mtRoute'])
.factory('routeConfiguration', function() {
  return {
    rootPath: 'views',
    extension: '.html'
  }
});
and link
<a href="/#/organization/person/">
after clicking there will be server request for view
/views/organization/person.html
Assuming that person.html contains
ng-controller="PersonController"
it may use person's id
var idPerson = $routeParams.idEntity;

Views configuration

You may define your views by providing routeConfiguration service.

Parameter Description
routeConfiguration.rootPath Defines base url for view definition lookup
routeConfiguration.extension Defines view url extension. It will be concatenated to every view url searched.

How routing works

There are 4 default routing strategies matched in order as described below. Each basic view has to be placed in directory. It cannot be subdirectory, or base directory. It enforces splitting views in directories. Standard link looks like this: <a href="/#/organization/person/">. Here we have organization directory in which person view file will be looked up. $routeParams.idEntity parameter will contain value of .

Route matches parameters

Part Role
:dir Directory in views rootPath in which view is placed
:idView Name of the view. It will be concatenated with extension before sending request
:idEntity Value that will be accessible to controller by $routeParams.idEntity
:parent Parent entity name.
:idParentEntity Value that will be accessible to controller by $routeParams.idParentEntity

Route matches

  1. /:dir/:parent/:idParentEntity/:idView/:idEntity
  2. /:dir/:parent/:idParentEntity/:idView
  3. /:dir/:idView/:idEntity
  4. /:dir/:idView

Automatic errors handling

Module provides interceptor for handling error status codes:

Code Type Action
401 unauthorized $rootScope.loggedIn property is set to false when unauthorized status is returned
403 forbidden is logged if possible
500 internal server error shows information in toastr panel

License

This project is licensed under terms of Apache 2.0 License