1

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

cssBlaster21895
  • 3,670
  • 20
  • 33
cengo
  • 151
  • 1
  • 6

2 Answers2

1

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

Ajit Soman
  • 3,926
  • 3
  • 22
  • 41
0
$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>
Dhaarani
  • 1,350
  • 1
  • 13
  • 23