Created
June 29, 2017 18:48
-
-
Save sycobuny/ca8b7bdcdffccb86b7563e16e090539a to your computer and use it in GitHub Desktop.
Which way is prettier? (Or "better", if not prettier)
This file contains 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
// 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) |
This file contains 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
// 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) |
This file contains 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
// 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