I am new to AngularJS and web development in general so this may be a stupid question but I cannot figure it out.
I have my main app
/// <reference path="../../references.ts" />
angular.module("mainApp", [])
.config(($routeProvider: ng.IRouteProvider) => {
$routeProvider
.when("/", {
templateUrl: "/app/module/mainApp/partials/main.html",
controller: AppController,
//datacontext
controllerAs: "dc"
})
.when("/login", {
templateUrl: "/app/module/mainApp/partials/login.html",
controller: LoginController,
controllerAs: "dc",
})
})
.run(($rootScope: ng.IRootScopeService, $location: ng.ILocationService, AuthService) => {
$rootScope.$on("$routeChangeStart", (event, next, current) => {
if (!AuthService.IsLoggedIn()) {
if (next.templateUrl == "app/module/mainApp/partials/login") {
return;
} else {
$location.path("/login");
}
}
});
})
My auth service looks like this. Alert in the constructor is never called
/// <reference path="../../../references.ts" />
class AuthService {
constructor(public ServiceManager: IServiceManager, public TokenHandler) {
alert("I am a constructor");
}
Login(loginRequest: Models.LoginRequest, successCallback) {
var params = {
username: loginRequest.username,
password: loginRequest.password
};
var service = this.ServiceManager.CallGetWithoutToken("http://url...", params);
service.success((data, status, headers, config) => {
this.TokenHandler.SetToken(data.replace(reg, ''));
});
};
IsLoggedIn() {
return this.TokenHandler.GetToken() != undefined;
}
}
angular.module("mainApp").factory("AuthService", () => AuthService.prototype);
and finally my TokenHandler is this
class TokenHandler {
token = undefined;
constructor() {
}
ClearToken() {
this.token = undefined;
};
SetToken(sessionToken: string) {
this.token = sessionToken;
};
GetToken() {
return this.token;
};
}
angular.module("mainApp").factory("TokenHandler", () => TokenHandler.prototype);
All my .ts files are in references.ts and I have added .js files to index.html.
When I run this I get an error cannot call method "GetToken()" of undefined
Should'nt AngularJS inject my TokenHandler in AuthService?