TypeScriptで書かれたサービスがあります。このサービスを利用Typescript AngularJS 'this'は定義されていません
export default class AServiceClass {
constructor(private $http: ng.IHttpService, ...) {}
method1(data : any) : void {
this.$http(...)
}
method2(data : any) : void {
this.$http(...)
}
}
そして、私はコントローラクラスを持っている、::ような何か - this is undefind
export default class AControllerClass {
constructor(private aService: AServiceClass, ...) {}
$onInit(){
let method = true ? this.aService.method1 : this.aService.method2;
method({ ... data ... });
}
}
サービスメソッドは例外をスローします。 this.aService.method1({ ... data ... })
のように使用すると、すべて問題ありません。私はmethod.bind(aService)({ ... data ... })
またはmethod.call(aService, { ... data ... })
を行うことができますが、なぜスコープの違いがありますか?
Thx。
この代わりに$ ngInitののOnInit $を、動作するはずです。角度1が私のために少し戻っているのは分かりませんが、試してみてください。これは$ onInit = function(){あなたのコード}です。 – Doomenik
ああ、これは、例ではtypoだった、ofc $ onInit。 Thx – Kindzoku
それでも "これは未定"です。 – Doomenik