Skip to content

Instantly share code, notes, and snippets.

@lucasconstantino
Created July 17, 2014 06:19
Show Gist options
  • Save lucasconstantino/022f4c288211d5aa3803 to your computer and use it in GitHub Desktop.
Save lucasconstantino/022f4c288211d5aa3803 to your computer and use it in GitHub Desktop.
Sample modularization in Angular applications.
/**
* ------------------------------------------------------------------------
* Main Application Module (app.js)
* ------------------------------------------------------------------------
* This script is responsible for instantiating the main application
* angular module and grabbing everything together.
*/
angular.module('MyApp', [
// Contributed modules.
'ui-router',
'ngAnimate',
// Custom modules.
'beer'
])
// Some globally available settings.
.constant('AppSettings', {
name: 'MyApp',
description: 'A very clever application, I suppose',
version: '1.4'
})
// Some global configurations. Usually configure some database.
.config(function () {})
// Some global after-bootstrap code.
.run(function () {
});
/**
* ------------------------------------------------------------------------
* Beer Controller (beer/controller/beer.js)
* ------------------------------------------------------------------------
*/
angular.module('beer')
.controller('BeerController', function ($scope, Beers) {
$scope.beers = Beers.getList().$object;
// Hey, you drunk!
$scope.drinkBeer = function (beer) {
beer.remove();
};
});
/**
* ------------------------------------------------------------------------
* Beer Module Manifest (beer/module.js)
* ------------------------------------------------------------------------
* Beer module is a nice module that can make you drunk.
*/
angular.module('beer', [
'restangular'
]);
/**
* ------------------------------------------------------------------------
* Beers REST Service (beer/services/beer.js)
* ------------------------------------------------------------------------
*/
angular.module('beer')
.factory('Beers', function (Restangular) {
return Restangular.all('beer');
});
/**
* ------------------------------------------------------------------------
* Main Router (router.js)
* ------------------------------------------------------------------------
* This should probably only be used to define global interest routes.
* Other routes would then be configured by the child modules as needed.
*/
angular.module('MyApp')
.config(function ($stateProvider, $urlRouterProvider) {
// Redirect to home if URL not found.
$urlRouterProvider.otherwise('/');
// Configure my global routes.
$stateProvider
.state('home', {
url: '/',
templateUrl: 'app/partials/home.html'
})
.state('contact', {
url: '/contact',
templateUrl: 'app/partials/contact.html'
controller: 'ContactFormController'
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment