在angularjs控制器的打字稿代码中处理$ scope

高拉夫·夏尔马

在$ scope和$ mdDialog是静态的情况下,以下工作。

declare var module: any;
export interface IChangePassword extends ng.IScope {
    cancel: Function;
    myname: string;
    state: string;
    processRequest: Function;
    CurrentPassword: string;
    NewPassword: string;
    ConfirmPassword: string;
}

export class ChangePasswordController {
    static $inject = ["$scope", "$mdDialog","$timeout"];
    static $mdDialog: any;
    static $timeout: any;
    state: string = 'getInput';
    static $scope: IChangePassword;
    CurrentPassword: string;
    NewPassword: string;
    ConfirmPassword: string;
    ChangePasswordService: any;

    constructor($scope: IChangePassword, $mdDialog: any, $timeout: any) {
        ChangePasswordController.$mdDialog = $mdDialog;
        ChangePasswordController.$scope = $scope;
        $scope.state = this.state;
        $scope.cancel = this.cancel;
        $scope.processRequest = this.processRequest;
       //this.ChangePasswordService = ChangePasswordService;
        ChangePasswordController.$timeout = $timeout;
    }

   public cancel ():void {
        ChangePasswordController.$mdDialog.cancel();
    };

    public processRequest(): void {
        ChangePasswordController.$scope.state = 'processRequest';
        ChangePasswordController.$timeout(function () {
            ChangePasswordController.$scope.state = 'Done';
        }, 5000);

    }


}

module.exports = function (app) {
    app.controller("ChangePasswordController", ChangePasswordController);
}

以下无效。在processRequest()中,对象this。$ scope始终为null。请帮忙。

export class ChangePasswordController {
    static $inject = ["$scope", "$mdDialog","$timeout"];
    static $mdDialog: any;
    static $timeout: any;
    state: string = 'getInput';
    $scope: IChangePassword;
    CurrentPassword: string;
    NewPassword: string;
    ConfirmPassword: string;
    ChangePasswordService: any;
    //myname: string = 'gaurav';
    constructor($scope: IChangePassword, $mdDialog: any, $timeout: any) {
        ChangePasswordController.$mdDialog = $mdDialog;
        this.$scope = $scope;
        $scope.state = this.state;
        $scope.cancel = this.cancel;
        $scope.processRequest = this.processRequest;
       //this.ChangePasswordService = ChangePasswordService;
        ChangePasswordController.$timeout = $timeout;
    }

   public cancel ():void {
        ChangePasswordController.$mdDialog.cancel();
    };

    public processRequest(): void {
        this.$scope.state = 'processRequest';
        ChangePasswordController.$timeout(function () {
            this.state = 'Done';
        }, 5000);

    }


}

module.exports = function (app) {
    app.controller("ChangePasswordController", ChangePasswordController);
}

我不确定为什么$ scope无法用作实例变量。在这段代码中。请让我知道是否有编写此代码的更好方法。

谢谢

提香·切尔尼科娃·德拉戈米尔

问题是您传递processRequest该函数时$scope未绑定当前对象this在Javascript中,this由调用者确定,而不由声明上下文确定。您可以使用bind将当前对象绑定到函数,或者使用this从声明上下文捕获的箭头函数

$scope.processRequest = this.processRequest.bind(this);
$scope.processRequest = () => this.processRequest();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

AngularJS控制器中的'this'与$ scope

在AngularJS中的不同控制器中访问$ scope

别名AngularJS $ scope对象在控制器中

Angularjs“控制器为”或“ $ scope”

在指令/控制器中绑定$ scope

在SpringMVC控制器层中,@Scope(“ prototype”)与@Scope(“ singleton”)

AngularJS:使用$ scope。$ watch与控制器作为语法

AngularJS-将$ scope传递给控制器

在AngularJS控制器和指令中使用“ this”而不是“ scope”

来自$ scope的AngularJS以编程方式获取表单控制器

AngularJS:通过属性从TD中的指令获取文本并通过控制器$ scope中的引发事件?

如何在AngularJS中与另一个控制器共享$ scope变量?

AngularJS如何在2个或更多控制器中解析对$ scope变量的调用?

AngularJS 控制器 $scope 在 ng-if 指令中不起作用

AngularJS控制器as和this而不是$ scope在指令中调用$ apply

从同一控制器Angularjs中的另一个方法访问$ scope变量

AngularJS:指令将对象添加到 $scope 但在控制器中未定义

scope()函数在AngularJS控制器中返回未定义

如何从Angularjs中的控制器向$ scope.master对象添加一些变量

AngularJS:为什么在我的ES6控制器中未定义$ scope?

AngularJS $ scope.foo设置了服务,但后来在同一控制器中未定义

在控制器函数的回调中修改$ scope

$ scope.data在控制器中未定义

将控制器$ scope绑定到服务中的对象

$ scope在控制器中未定义

角$ scope变量未在控制器中更新

没有$ scope的控制器

从服务刷新特定控制器$ scope

带打字稿的angularjs-无法访问$ scope