我正在使用角度ui路由器。我想用base64編碼對$stateParams
進行編碼。例如:
http://example.com/profile/6013/details
到
http://example.com/profile/kfnvjodu==/details
我正在使用角度ui路由器。我想用base64編碼對$stateParams
進行編碼。例如:
http://example.com/profile/6013/details
到
http://example.com/profile/kfnvjodu==/details
$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>
要編碼爲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