I'm using angular ui-router. I would like to encode the $stateParams
with base64 encoding. For example:
http://example.com/profile/6013/details
to
http://example.com/profile/kfnvjodu==/details
I'm using angular ui-router. I would like to encode the $stateParams
with base64 encoding. For example:
http://example.com/profile/6013/details
to
http://example.com/profile/kfnvjodu==/details
To encode a string in base64 format we use btoa()
function and to decode the same encoded string we use atob()
function.
Example:
var x="angular js";
var encodedString = btoa(x); // result: YW5ndWxhciBqcw==
var decodedString = atob(encodedString);// result: angular js
So if you encode like this "http://example.com/profile/"+btoa(6013)+"/detials"
will result you this url http://example.com/profile/NjAxMw==/detials
$scope.go = function (params) {
$location.path(decodeURIComponent(params));
};
<!DOCTYPE html>
<html>
<body>
<p>Click the button to encode a URI.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var uri = "my test.asp?name=ståle&car=saab";
var res = encodeURI(uri);
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>