Encapsulation of Nick Galbreath's base64.js library for AngularJS
For Base64 encoding whch supports UTF8 see angular-utf8-base64
bower install angular-base64
NB: The ngBase64
bower package is deprecated due to camel casing issues on case-sensitive file systems.
<script src="bower_components/angular-base64/angular-base64.js"></script>
angular
.module('myApp', ['base64'])
.controller('myController', [
'$base64', '$scope',
function($base64, $scope) {
$scope.encoded = $base64.encode('a string');
$scope.decoded = $base64.decode('YSBzdHJpbmc=');
}]);
Base 64 encoded strings can be made "URL Safe" with encodeURIComponent
...
var base64EncodedString = $base64.encode('whatever');
var urlSafeBase64EncodedString = encodeURIComponent(base64EncodedString);
To decode a "URL Safe" base 64 encoded string use decodeURIComponent
before $base64.decode
...
var base64EncodedString = decodeURIComponent('d2hhdGV2ZXI');
var decodedString = $base64.decode(base64EncodedString);