Javascript 通过';这';在类ecmascript 6中回调
我在ecmascript 6中有一个类。我需要将“this”的值传递给回调 我试过使用.bind(这个)。到目前为止似乎还不起作用。我还尝试设置var_this=this;并在回调中使用它。它仍然不起作用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
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
,那么我的第一个建议就行了。如果您希望模式
成为此
,请使用我的第二个建议。