title: Best practice writing AngularJS controller in Typescript
body: <p>I am really new to AngularJS and Web Development and more experienced in C# and C++.
  Because of that I try to use Typescript.</p>
  
  <p>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.</p>
  
  <pre><code>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")
    }
  }
  </code></pre>
  
  <p>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? </p>
  
  <p>Thanks in advance.</p>