Skip to content

Instantly share code, notes, and snippets.

@alatzl
Created May 11, 2015 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alatzl/b61b4c747ce3bdfa0215 to your computer and use it in GitHub Desktop.
Save alatzl/b61b4c747ce3bdfa0215 to your computer and use it in GitHub Desktop.
Wrap Global Variables in Services
// Instead of this:
angular.module('app', [])
.controller('MyCtrl', function($scope) {
// What is moment? How do I know this exists and is the right object?
$scope.startTime = moment();
$scope.endTime - moment().add(1, 'h');
});
// Try this:
angular.module('app', [])
.service('moment', function($window) {
return $window.moment();
})
.controller('MyCtrl', function($scope, moment) {
$scope.startTime = moment();
$scope.endTime = moment().add(1, 'h');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment