Angular 传入角模板的回调函数无法访问组件
回调函数无权访问组件中的任何内容。我想我必须绑定“这个”,但我有点不知道在哪里可以绑定。 在我的html模板中,我有一个按钮,我在其中传递回调函数作为参数:Angular 传入角模板的回调函数无法访问组件,angular,callback,this,angular-template,Angular,Callback,This,Angular Template,回调函数无权访问组件中的任何内容。我想我必须绑定“这个”,但我有点不知道在哪里可以绑定。 在我的html模板中,我有一个按钮,我在其中传递回调函数作为参数: <button (click)="primaryAction('test',furtherAction1)">Action<button> FutureAction1无权访问此.FutureAction1One或此.FutureAction2()。 现在,我如何在回调函数中使用正确的“this”?这只是一个范围问
<button (click)="primaryAction('test',furtherAction1)">Action<button>
FutureAction1无权访问此.FutureAction1One或此.FutureAction2()。现在,我如何在回调函数中使用正确的“this”?这只是一个范围问题 要将上下文重新绑定到函数,请使用
call
primaryAction(string,callback){
this.primaryActionDone = "Done";
callback.call(this)
}
您必须在
primaryAction
函数中调用callback.call(this)
。这只是一个作用域问题,在JS中很常见。或者FutureAction1=()=>{
perfect@trichetriche我用bind(这个)试过了,但我显然需要调用(这个)。请回答这个问题,这样我就可以接受它。@yurzui你不能在中使用lambdastemplates@TimarIvoBatis这只是你的输入错误,只需删除你的问题:)
primaryAction(string,callback){
this.primaryActionDone = "Done";
callback.call(this)
}