Supun Wijerathne
Supun Wijerathne

Reputation: 12948

encode-decode base64 string AngularJS1 TypeScript?

I am using Angular1 with TypeScript. I have a simple question.

Is there any way to encode-decode a string in base64 with the environment, I use.?

I have searched a lot, I could find this. But I am not sure whether it is available with the Type-definition.

Upvotes: 2

Views: 2571

Answers (1)

Ross Scott
Ross Scott

Reputation: 1732

So following the example from https://github.com/ninjatronic/angular-base64 but with a bit of TS decoration:

angular
    .module('myApp', ['base64'])
    .controller('myController', [

        '$base64', '$scope', 
        function($base64: any, $scope : ng.IScope) {

            $scope.encoded = $base64.encode('a string');
            $scope.decoded = $base64.decode('YSBzdHJpbmc=');
    }]);

So all I've done here is said that the $base64 parameter is "any", this allows you to use regular JS against it without TS complaining. You won't get intellisense / type checking against it, but that doesn't matter. As I said in the comment if you do want to define your own interface against it you could. i.e

interface Base64Encode {
    encode: (source: string) => string;  
    decode: (encoded: string) => string;
}

Upvotes: 2

Related Questions