繁体   English   中英

我无法在$ Typeject AngularJS类中使用$ inject注释

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

我正在尝试让$ inject注释在我的Typescript类上起作用,但是它似乎始终是未定义的。 谁能说出我在做什么错?

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 ] );

如果删除$ inject注释并将控制器创建更改为此:

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

有用。

(我知道PicasaAuthenticator可能应该是Single,并且应定义为服务)

非常感谢

这是因为在注册服务时,将再次使用方括号表示法覆盖静态$inject属性中提供的注入参数。 此处再次使用括号表示法,以其他语法指定显式依赖项注释,并且在您的案例中未列出任何注入参数。 因此,您不会看到角度DI过程注入了任何东西。

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

做就是了:-

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM