Created
May 11, 2015 16:20
-
-
Save alatzl/b61b4c747ce3bdfa0215 to your computer and use it in GitHub Desktop.
Wrap Global Variables in Services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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