Ich habe einen Service mit TypeScript geschrieben. Etwas wie:Typescript AngularJS 'dies' ist undefined
export default class AServiceClass {
constructor(private $http: ng.IHttpService, ...) {}
method1(data : any) : void {
this.$http(...)
}
method2(data : any) : void {
this.$http(...)
}
}
Und ich habe eine Controller-Klasse, die diesen Dienst nutzen:
export default class AControllerClass {
constructor(private aService: AServiceClass, ...) {}
$onInit(){
let method = true ? this.aService.method1 : this.aService.method2;
method({ ... data ... });
}
}
Servicemethoden werfen eine Ausnahme - this is undefind
. Wenn ich es wie this.aService.method1({ ... data ... })
benutze, ist alles in Ordnung. Ofc kann ich method.bind(aService)({ ... data ... })
oder method.call(aService, { ... data ... })
tun, aber warum gibt es einen Unterschied im Bereich?
Thx.
diese statt $ ngInit OnInit $ sollte, arbeiten. Nicht sicher, Winkel 1 ist schon ein bisschen zurück für mich, aber versuchen Sie es. dies. $ onInit = function() {Ihr Code}; – Doomenik
Ah, es war ein Tippfehler im Beispiel von $ onInit. Thx – Kindzoku
Noch wirft es "das ist nicht gelöst"? – Doomenik