angulartypescriptnpm-install

base 64 encode and decode a string in angular (2+)


How to encode or decode a string in angular 2 with base64 ??? My front-end tool is Angular 2. I had a password string, before passing it to API I need to base64 encode. Since in service base64 encoded string will be decoded.

So I am looking for some base64 encode/decode library for Angular2/Typescript and some options.

Thanks!!!


Solution

  • Use the btoa() function to encode:

    console.log(btoa("password")); // cGFzc3dvcmQ=

    To decode, you can use the atob() function:

    console.log(atob("cGFzc3dvcmQ=")); // password