Roaders
Roaders

Reputation: 4525

I can't get the $inject annotation to work in my Typescript AngularJS Class

I am trying to get the $inject annotation to work on my Typescript class but it always seems to be undefined. Can anyone suggest what I'm doing wrong?

export class PicasaAuthenticator {

    static $inject = [ '$window' ];
    constructor( private $window : ng.IWindowService )
    {
    }

    public startAuthentication()
    {
        var authUrl:string = "https://accounts.google.com/o/oauth2/auth";
        authUrl += "?client_id=" + client_id;
        authUrl += "&response_type=token";
        authUrl += "&scope=https://picasaweb.google.com/data/";
        authUrl += "&redirect_uri=" + window.location;

        if ( this.$window )
        {
            this.$window.location.assign( authUrl );
        }
        else
        {
            alert( "nav: " + authUrl );
        }
    }
}

app.controller('AuthenticationControl', [ Picasa.PicasaAuthenticator ] );

If I remove the $inject annotation and change my controller creation to this:

app.controller('AuthenticationControl', [ '$window', Picasa.PicasaAuthenticator ] );

It works.

(I know that PicasaAuthenticator should probably be a Single and be defined as a service)

Many Thanks

Upvotes: 0

Views: 319

Answers (1)

PSL
PSL

Reputation: 123739

That is because you are overriding the injection arguments provided in the static $inject property with the usage of bracket notation again while registering the service. The usage of bracket notation there again specifies explicit dependency annotation with alternate syntax and it does not list any injection argument in your case. So you don't see any thing getting injected by the angular DI process.

app.controller('AuthenticationControl', [ Picasa.PicasaAuthenticator ] );
                                        // ^__ Remove this

Just do:-

 app.controller('AuthenticationControl', Picasa.PicasaAuthenticator);

Upvotes: 3

Related Questions