如何使用Angular Material Dialog调用控制器上的函数

时间:2017-01-02 13:50:13

标签: angularjs angular-material

我有一个使用角度材料的角度1.5打字稿的应用程序。

如何通过Dialog调用控制器中的函数?

在我的例子中,我想在用户确认后调用this.callBack()

代码段

 public delete(condition: ModelModule.Condition): void {
        var confirm = this.$mdDialog.confirm()
            .title('delete condition!')
            .textContent('are you sure ?')
            .ariaLabel('delete')
            .ok('Ok')
            .cancel('Cancel');

        this.$mdDialog.show(confirm).then(function(answer) {
             console.log("You decided to delete "+answer)
        // how to call this function on my controller ???              
        this.callBack()

        }, function() {
            console.log("You decided cancel")
        });
    }

1 个答案:

答案 0 :(得分:0)

您的this是指function,而不是您的控制人。不要在Typescript中使用function,而是使用箭头函数,它不会创建新的此上下文。

public delete(condition: ModelModule.Condition): void {
    var confirm = this.$mdDialog.confirm()
        .title('delete condition!')
        .textContent('are you sure ?')
        .ariaLabel('delete')
        .ok('Ok')
        .cancel('Cancel');

    this.$mdDialog.show(confirm).then((answer) => {
         console.log("You decided to delete " + answer);
         this.callBack();
    }, () => {
        console.log("You decided cancel");
    });
}

this in Typescript