Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Last active May 13, 2016 17:40
Show Gist options
  • Save sycobuny/539eb68bb58da7d390f77e595eed41e0 to your computer and use it in GitHub Desktop.
Save sycobuny/539eb68bb58da7d390f77e595eed41e0 to your computer and use it in GitHub Desktop.
Comparing two potential ways to solve the "attach a 'global' function to my Angular app" question
myApp.factory('queryStringFactory', [function() {
return function(obj) {
var params = []
for (var key in obj) {
params.push(
encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])
)
}
return params.join('&')
}
}])
myApp.controller('linkerCtrl', ['$scope', 'queryStringFactory', function($s, qs) {
$s.getLink = function() {
return 'some-page.html?' + qs({
key1: $s.someModel.key1,
key2: $s.someModel.key2
})
}
}])
myApp.queryString = function(obj) {
var params = []
for (var key in obj) {
params.push(
encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])
)
}
return params.join('&')
}
myApp.controller('linkerCtrl', ['$scope', function($s) {
$s.getLink = function() {
return 'some-page.html?' + myApp.queryString({
key1: $s.someModel.key1,
key2: $s.someModel.key2
})
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment