我有一个使用角度材料的角度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")
});
}
答案 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");
});
}