'this'在function()中未定义.角度2 [英] 'this' is undefined inside function(). angular 2
本文介绍了'this'在function()中未定义.角度2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我在我的应用中使用了 uikit确认模式.我的问题是,当我要单击<button>
进行确认时.内部函数this
是undefined
.这是我的代码...
So I am using a uikit confirmation modal in my app. My problem is, when I am going to click the <button>
for confirmation. the this
inside function is undefined
. here's my code...
declare var UIkit:any;
deleteData(dataArr): void {
UIkit.modal.confirm('Are you sure you want to delete this?', function() {
console.log(dataArr);
console.log(this);
//use service here...
UIkit.modal.alert('Confirmed!');
});
}
在该函数中,我希望对http请求使用服务,但在this
上出现问题.我正在使用Angular2.x.
Inside that function I wish to use service for http request but I am having a problem on the this
. I am using Angular 2.x.
推荐答案
使用箭头功能...
declare var UIkit:any;
deleteData(dataArr): void {
UIkit.modal.confirm('Are you sure you want to delete this?', () => {
console.log(this);
// [...]
});
}
查看 MDN:箭头函数有关此事的详细信息.
Check out MDN: Arrow functions for details on the matter.
箭头函数不会创建自己的上下文,因此
this
具有 它的原始含义来自封闭的上下文.
An arrow function does not create its own this context, so
this
has its original meaning from the enclosing context.
这篇关于'this'在function()中未定义.角度2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文