typescript 我如何在打字稿构造函数之外正确使用 angular $timeout 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28472561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:57:47  来源:igfitidea点击:

How do i correctly use angular $timeout service outside of a typescript constructor?

angularjstypescript

提问by XT_Nova

I'm trying to figure out how to correctly use the angular $timeout service with TypeScript - So that i can show a splash-screen-like-behavior when someone for example clicks on a "Log In"-button.

我试图弄清楚如何正确使用带有 TypeScript 的 angular $timeout 服务 - 这样我就可以在某人单击“登录”按钮时显示类似闪屏的行为。

interface INavigationController {
        username: string;
        password: string;
        loggedIn: boolean;
        ready: boolean;
        signIn(): void;
        signOut(): void;
    }

    class NavigationController implements INavigationController {
        username: string;
        password: string;
        loggedIn: boolean;
        ready: boolean;

        static $inject = ['$timeout'];
        constructor(private $timeout: ng.ITimeoutService) {
            var vm = this;
            vm.username = this.username;
            vm.password = this.password;
            vm.ready = true;

            // The line below work just fine from within here - but i want the behavior
            // when someone clicks a button, not directly like this.
            // Remove vm.ready = true above, before...
            //$timeout(function () { vm.ready = true;}, 2200);}

        signIn() {
            if (!this.username || !this.password) {
                alert("You need to frickin enter som details, man!");
            } else {
                this.ready = false;
                this.$timeout(function () {
                    this.ready = true;
                }, 2200);
            }
        }

I have also tried to set the invokeApply?:booleanto true:

我还尝试将 设置invokeApply?:boolean为 true:

this.$timeout(function () {
    this.ready = true;
}, 2200, true);

- Without any success...

- 没有任何成功...

If i console.log(this.ready);from within the this.$timeout(function()...it will show me trueafter 2200ms, but it seems that the $apply()doesn't apply?

如果我console.log(this.ready);从内部this.$timeout(function()...它会true在 2200 毫秒后显示给我,但似乎$apply()不适用?

Would appreciate if someone could enlighten me in how to use the $timeout and other services like it, correctly - Preferably using the style-guide i'm aiming for i.e. using "controllerAs with vm" - John Papa

如果有人能启发我正确地使用 $timeout 和其他类似的服务,我将不胜感激 - 最好使用我的目标风格指南,即使用“controllerAs with vm” - John Papa

回答by lujcon

this.$timeout(()=> {
                this.ready = true;
            }, 2200);

if you use function(){} pattern you don't have access to this(thisis not the one you expect)

如果您使用 function(){} 模式,则您无权访问thisthis不是您期望的模式)

回答by Peter Ashwell

The 'this' inside the timeout is the this of the timeout function. You need to define a variable outside to reference that scope (usually called 'that') and refer to it inside the timeout:

超时内的'this'是超时函数的this。您需要在外部定义一个变量来引用该范围(通常称为“那个”)并在超时内引用它:

var that = this;
$timeout(function() {
     that.value ='eggs';
}, 2200);

回答by Hall Wong

try this, mate

试试这个,伙计

var obj = this;
this.$timeout(function () {
  obj.ready = true;
  }, 2200);