Angularjs ES6类在类中使用bind vs call调用方法?

Angularjs ES6类在类中使用bind vs call调用方法?,angularjs,data-binding,angularjs-scope,ecmascript-6,es6-class,Angularjs,Data Binding,Angularjs Scope,Ecmascript 6,Es6 Class,我有一个用ES6编写的类,还有一个指令“action”,它需要访问一个名为“selected”的控制器值。此控制器值“选定”由另一个指令“grid”更新。(双向绑定) 我需要在select上将“selected”值从控制器传递到指令“actions”,该控制器已由指令“grid”更新。我试图通过执行“绑定”来传递,但我得到一个类型错误,即“无法读取未定义的actionHandler” 我不确定处理此问题的最佳方法是什么,例如,当“grid”指令更新了“selected”值时,actionEven

我有一个用ES6编写的类,还有一个指令“action”,它需要访问一个名为“selected”的控制器值。此控制器值“选定”由另一个指令“grid”更新。(双向绑定)

我需要在select上将“selected”值从控制器传递到指令“actions”,该控制器已由指令“grid”更新。我试图通过执行“绑定”来传递,但我得到一个类型错误,即“无法读取未定义的actionHandler”

我不确定处理此问题的最佳方法是什么,例如,当“grid”指令更新了“selected”值时,actionEvent将使用控制器的更新值触发。指令工作正常,我可以看到它在断点处中断

以下是我在HTML中的内容

<div class="col-xs-9">          
   <action options="ctrl.Actions" on-select="ctrl.actionEvent">
   </action>
</div>
<div class="col-xs-12">
  <grid config="ctrl.gridOptions" data="ctrl.data" selected="ctrl.selected"></grid>
 </div>

首先,不要混淆
.bind()
.call()

  • First返回一个新的函数实例,稍后可以调用该实例,但保留
    this
  • 第二次调用立即起作用,但仅为此调用修改
    的上下文
阅读了解更多信息

您正在传递对
actionEvent
方法的引用。在调用时,对原始控制器对象的引用已经丢失

要保留引用,首先需要将其保存在构造函数中

class CheckC {
  constructor($scope) {
    this.$scope = $scope;       
    this.selected = this.$scope.selected;
    //bind method here
    this.actionEvent = this.actionEvent.bind(this);
  }

  actionEvent(item, event) {
    // this here will be always contain a referene to class instance
    this.actionHandler(item, event);
  }

  actionHandler(item, event) {
    let selection;
    let model = this.selected;
    if(model) {
      item.id = 1;
    }
  }
}

此外,在您的代码
actionEvent
中,方法似乎是多余的。考虑重新编译代码并直接通过<代码> ActhActhRunter 。(不要忘记更新
.bind()
调用,它应该在调用后绑定
actionHandler

首先,不要混淆
.bind()
调用()

  • First返回一个新的函数实例,稍后可以调用该实例,但保留
    this
  • 第二次调用立即起作用,但仅为此调用修改
    的上下文
阅读了解更多信息

您正在传递对
actionEvent
方法的引用。在调用时,对原始控制器对象的引用已经丢失

要保留引用,首先需要将其保存在构造函数中

class CheckC {
  constructor($scope) {
    this.$scope = $scope;       
    this.selected = this.$scope.selected;
    //bind method here
    this.actionEvent = this.actionEvent.bind(this);
  }

  actionEvent(item, event) {
    // this here will be always contain a referene to class instance
    this.actionHandler(item, event);
  }

  actionHandler(item, event) {
    let selection;
    let model = this.selected;
    if(model) {
      item.id = 1;
    }
  }
}

此外,在您的代码
actionEvent
中,方法似乎是多余的。考虑重新编译代码并直接通过<代码> ActhActhRunter 。(不要忘记更新
.bind()
调用,它应该在调用后绑定
actionHandler

尝试将
记录在
actionEvent
函数中,它将显示什么?“指令工作正常”,但不是它们应该显示的<选择
时的code>应使用类似于
ctrl.actionEvent()
的表达式。这也可以解决您的问题。将绑定的方法重新分配给构造函数中的类实例:
this.actionHandler=this.actionHandler.bind(this)
。仅在
actionEvent
中调用
this.actionHandler.bind(this)
不会调用该方法,只会创建一个新函数,当
actionEvent
退出时,该函数会被丢弃,因为绑定的方法没有被任何引用。尝试将
this
记录在
actionEvent
函数中,它会显示什么?“指令工作正常”,但不正常。select上的
应使用类似
ctrl.actionEvent()
的表达式。这也会解决您的问题。将绑定的方法重新分配给构造函数中的类实例:
this.actionHandler=this.actionHandler.bind(this)
。只需在
actionEvent
中调用
this.actionHandler.bind(this)
不会调用该方法,只需创建一个新函数,当
actionEvent
退出时,该函数将被丢弃,因为绑定的方法未被任何对象引用。