Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Created June 29, 2017 18:48
Show Gist options
  • Save sycobuny/ca8b7bdcdffccb86b7563e16e090539a to your computer and use it in GitHub Desktop.
Save sycobuny/ca8b7bdcdffccb86b7563e16e090539a to your computer and use it in GitHub Desktop.
Which way is prettier? (Or "better", if not prettier)
// describe the constants as angular constants, namespaced to the controller that uses them
(function(angular, undefined) {
angular.module('myapp').constant('MyCtrl.API_ENDPOINT', 'https://myserver.com/data.json')
})(angular)
// define the controller and inject the relevant constants for that controller
(function(angular, undefined) {
angular.module('myapp').controller('MyCtrl', MyCtrl)
MyCtrl.$inject = ['MyCtrl.API_ENDPOINT', '$http']
function MyCtrl(API_ENDPOINT, $http) {
var vm = this
vm.doTheThing = doTheThing
function doTheThing() {
return $http.get(API_ENDPOINT)
}
}
})(angular)
// define the constants as regular variables inside the controller that uses them
(function(angular, undefined) {
angular.module('myapp').controller('MyCtrl', MyCtrl)
var API_ENDPOINT = 'https://myserver.com/data.json'
MyCtrl.$inject = ['$http']
function MyCtrl($http) {
var vm = this
vm.doTheThing = doTheThing
function doTheThing() {
return $http.get(API_ENDPOINT)
}
}
})(angular)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment