I am really new to AngularJS and Web Development and more experienced in C# and C++. Because of that I try to use Typescript.
I am working on a controller right now and got some strange behavior. When using the login-function like in the code below a have the problem, that the services sessionService and userDataService are not defined inside the then-statement. It is accessible before and after the then.
class UserController {
scope: UserControllerScope;
location: ng.ILocationService;
sessionService: SessionService;
userDataService: UserDataService;
constructor($scope: UserControllerScope,
$location: ng.ILocationService,
userDataService: UserDataService,
sessionService: SessionService){
this.scope = $scope;
this.location = $location;
this.userDataService = userDataService;
this.sessionService = sessionService;
}
loginUser(username: string, pw: string) {
var foundUser;
console.log(this.sessionService);
console.log(this.userDataService);
console.log("Got into LoginUser");
this.userDataService.getUserAsync(username, pw).then(function(foundUserFromDb) {
console.log(this.sessionService);
console.log(this.userDataService);
});
console.log(this.sessionService);
console.log(this.userDataService);
this.location.path("views/projects.html")
}
}
Is there a way the custom services needs to be injected so that they can be access everywhere in the class or do you have better ways to deal with the injection of services and controllers?
Thanks in advance.
this.scope = $scopeyou can just change the constructor parameter topublic $scope: UserControllerScopebut that's not what is causing your issue.