Last active
May 13, 2016 17:40
-
-
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
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
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 | |
}) | |
} | |
}]) |
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
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