Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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
Javascript 通过';这';在类ecmascript 6中回调_Javascript_Callback_Ecmascript 6_This - Fatal编程技术网

Javascript 通过';这';在类ecmascript 6中回调

Javascript 通过';这';在类ecmascript 6中回调,javascript,callback,ecmascript-6,this,Javascript,Callback,Ecmascript 6,This,我在ecmascript 6中有一个类。我需要将“this”的值传递给回调 我试过使用.bind(这个)。到目前为止似乎还不起作用。我还尝试设置var_this=this;并在回调中使用它。它仍然不起作用 class Modal { constructor(modal) { this._modal = modal; this.id = this._options.id; } } open(opt

我在ecmascript 6中有一个类。我需要将“this”的值传递给回调

我试过使用.bind(这个)。到目前为止似乎还不起作用。我还尝试设置var_this=this;并在回调中使用它。它仍然不起作用

     class Modal {
        constructor(modal) {
        this._modal = modal;
        this.id = this._options.id;      
       }
    }

    open(opts) {
        let modalOptions = {
            size: opts.size || '',
                templateUrl: 'modal.html',
                controller: function controller($scope, $uibModalInstance) {
                    var _this = this; 
                    this._options = {
                        id: opts.id
                    };
                    this.hcbuttons: [{id: '1', name: 'test'}, {id: '2', name: 'abc'}];
                    publisher.subscribe('triggered', this._options.id, function(event, creator) {
                        //as soon as we subscribe to the published event
                        var result = this.hcbuttons.filter(function( obj ) {
                            return obj.id == creator;
                        })[0];
                        if(result.sync === true) {
                            console.log('disabledall');
                        }                         
                    }).bind(this);


        }

} 

您错误地绑定了
。您正在通过
subscribe
函数的返回值调用绑定。函数对象在其原型中只有函数
bind
。因此,从这个
})中选择你的代码
}.bind(this))

由于要将此
设置为模态类

 //change one
 open(opts) {
        var _this = this;
        let modalOptions = {
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~
 //change two
 }.bind(_this));

您错误地绑定了
。您正在通过
subscribe
函数的返回值调用绑定。函数对象在其原型中只有函数
bind
。因此,从这个
})中选择你的代码
}.bind(this))

由于要将此
设置为模态类

 //change one
 open(opts) {
        var _this = this;
        let modalOptions = {
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~
 //change two
 }.bind(_this));

如果您使用的是ES2015,为什么不使用lambdas(箭头功能)?他们会自动绑定这个

open(opts) {
  let modalOptions = {
    size: opts.size || '',
    templateUrl: 'modal.html',
    controller: function controller($scope, $uibModalInstance) {
      this._options = {
        id: opts.id
      };
      this.hcbuttons = [{
        id: '1',
        name: 'test'
      }, {
        id: '2',
        name: 'abc'
      }];
      publisher.subscribe('triggered', this._options.id, (event, creator) => {
        let result = this.hcbuttons.filter(obj => obj.id === creator)[0];
        if (result.sync) {
          console.log('disabledall');
        }
      });
    }
  }
}

在这里,您可以阅读有关箭头函数的更多信息:以及它们如何工作(将来可能会对您有所帮助)。

如果您使用的是ES2015,为什么不使用lambdas(箭头函数)?他们会自动绑定这个

open(opts) {
  let modalOptions = {
    size: opts.size || '',
    templateUrl: 'modal.html',
    controller: function controller($scope, $uibModalInstance) {
      this._options = {
        id: opts.id
      };
      this.hcbuttons = [{
        id: '1',
        name: 'test'
      }, {
        id: '2',
        name: 'abc'
      }];
      publisher.subscribe('triggered', this._options.id, (event, creator) => {
        let result = this.hcbuttons.filter(obj => obj.id === creator)[0];
        if (result.sync) {
          console.log('disabledall');
        }
      });
    }
  }
}

在这里,您可以阅读更多有关箭头函数的内容:以及它们如何工作(将来可能会对您有所帮助)。

您希望这是什么?在modalinstance中,我基本上希望将“this.hcbuttons”传递给callback@looneytunes,请先修复缩进和括号,然后解释实际构建的内容。然后我们可以在同一级别上讨论
this.hcbuttons:[{id:'1',name:'test'},{id:'2',name:'abc'}]不是有效的语法。如果希望
this
引用
Modal
实例,则需要调用
var\u this=this
这个
引用索引的上下文中(当然不在
控制器
函数中)。您希望它是什么?在modalinstance中,我基本上希望将'this.hcbuttons'传递给callback@looneytunes,请先修复缩进和括号,然后解释实际构建的内容。然后我们可以在同一级别上讨论
this.hcbuttons:[{id:'1',name:'test'},{id:'2',name:'abc'}]不是有效的语法。如果希望
this
引用
Modal
实例,则需要调用
var\u this=this
引用索引的上下文中(该索引肯定不在
控制器
函数中)。尝试过它后,仍然不起作用@RajaprabhuAravindasamy@looneytunes如果您希望
modalOptions
成为该事件中的
this
,那么我的第一个建议就行了。如果您希望
模式
成为
的模式,请使用我的第二个建议。尝试过之后,仍然无法@Rajaprabhu工作Aravindasamy@looneytunes如果您希望
modalOptions
成为该事件中的
this
,那么我的第一个建议就行了。如果您希望
模式
成为
,请使用我的第二个建议。