2017-08-04 92 views
0

我正在使用角度ui路由器。我想用base64编码对$stateParams进行编码。例如:

http://example.com/profile/6013/details

http://example.com/profile/kfnvjodu==/details

回答

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>

0

要编码为Base64格式的字符串,我们使用btoa() FUNC并解码相同的编码字符串,我们使用atob()函数。

例子:

var x="angular js"; 
var encodedString = btoa(x); // result: YW5ndWxhciBqcw== 
var decodedString = atob(encodedString);// result: angular js 

所以,如果你编码这样"http://example.com/profile/"+btoa(6013)+"/detials"将导致你这个网址http://example.com/profile/NjAxMw==/detials