Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 传入角模板的回调函数无法访问组件_Angular_Callback_This_Angular Template - Fatal编程技术网

Angular 传入角模板的回调函数无法访问组件

Angular 传入角模板的回调函数无法访问组件,angular,callback,this,angular-template,Angular,Callback,This,Angular Template,回调函数无权访问组件中的任何内容。我想我必须绑定“这个”,但我有点不知道在哪里可以绑定。 在我的html模板中,我有一个按钮,我在其中传递回调函数作为参数: <button (click)="primaryAction('test',furtherAction1)">Action<button> FutureAction1无权访问此.FutureAction1One或此.FutureAction2()。 现在,我如何在回调函数中使用正确的“this”?这只是一个范围问

回调函数无权访问组件中的任何内容。我想我必须绑定“这个”,但我有点不知道在哪里可以绑定。

在我的html模板中,我有一个按钮,我在其中传递回调函数作为参数:

<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)
}