我想从外部调用 angular2 服务中的方法,并从该方法调用同一类中的其他方法或访问构造中初始化的变量。
我正在使用 ngZone 使方法可以从外部访问。我的课看起来像这样:
var Component1 = ng.core.Class({
constructor:[ng.core.NgZone, function(_ngZone) {
window.Component = {
onCallFromOutside: this.onCallFromOutside,
zone: _ngZone
};
this.some_var = "AAA";
}],
onCallFromOutside: function(){
console.log("Called onCallFromOutside")
console.log(this.some_var) // This is null
this.inisdeCall() // Here I get error: EXCEPTION: TypeError: this.inisdeCall is not a function
}
inisdeCall: function(){
console.log("Called inisdeCall")
}
})
我从外面这样调用这个方法:
window.Component.zone.run(function(){
window.Component.onCallFromOutside(e);
})
这是我得到的错误:
EXCEPTION: TypeError: this.inisdeCall is not a function
我在纯 JavaScript 中使用 angular2