TypeScript + AngularJS v1:将 angular 工厂重写为 TypeScript
TypeScript + AngularJS v1: Rewriting angular factory to TypeScript
我想知道如何将以下工厂重写为 TypeScript 代码。这是原始代码:
app.factory('errorInterceptor', function ($q) {
return {
responseError: function (response) {
console.error("Error: " + response.statusText);
return $q.reject(response);
}
}
});
到目前为止,我已经尝试了以下方法:
export class errorInterceptor {
constructor(private $q:ng.IQService) {
}
public responseError(response:any){
console.error("Error: " + response.statusText);
return this.$q.reject(response);
}
public static getFactory(){
return errorInterceptor;
}
}
app.factory('errorInterceptor',errorInterceptor.getFactory());
但我收到以下错误:
Provider 'errorInterceptor' must return a value from $get factory method.
有什么想法吗?
我使用这个语法:
export class errorInterceptor {
// to support minification
static $inject = ["$q"];
constructor(private $q:ng.IQService) {
}
public responseError(response:any){
console.error("Error: " + response.statusText);
return this.$q.reject(response);
}
//public static getFactory(){
// return errorInterceptor;
//}
}
//app.factory('errorInterceptor',errorInterceptor.getFactory());
app.service('errorInterceptor',errorInterceptor);
延长:
这是我用来拦截 $http
调用的片段 (所以它对我有用)
module MyModule
{
var app = angular.module("MyModule");
export class HttpErrorAspect
{
static $inject = ["$q"];
constructor(private $q: ng.IQService)
{
}
public responseError = (rejection: any): any =>
{
// do some magic, e.g. use toaster or alerter
// to notify about the issue
...
// reject that all
return this.$q.reject(rejection);
}
}
app.service("HttpErrorFilter", MyModule.HttpErrorAspect);
}
@service(app)
export class UserRolesService {
static $inject: string[] = ["$cookies"];
private $cookies: angular.cookies.ICookiesService;
user: IUser;
GetUser(): IUser {
return this.user;
}
}
致电:
this.userRolesService.GetUser()
我想知道如何将以下工厂重写为 TypeScript 代码。这是原始代码:
app.factory('errorInterceptor', function ($q) {
return {
responseError: function (response) {
console.error("Error: " + response.statusText);
return $q.reject(response);
}
}
});
到目前为止,我已经尝试了以下方法:
export class errorInterceptor {
constructor(private $q:ng.IQService) {
}
public responseError(response:any){
console.error("Error: " + response.statusText);
return this.$q.reject(response);
}
public static getFactory(){
return errorInterceptor;
}
}
app.factory('errorInterceptor',errorInterceptor.getFactory());
但我收到以下错误:
Provider 'errorInterceptor' must return a value from $get factory method.
有什么想法吗?
我使用这个语法:
export class errorInterceptor {
// to support minification
static $inject = ["$q"];
constructor(private $q:ng.IQService) {
}
public responseError(response:any){
console.error("Error: " + response.statusText);
return this.$q.reject(response);
}
//public static getFactory(){
// return errorInterceptor;
//}
}
//app.factory('errorInterceptor',errorInterceptor.getFactory());
app.service('errorInterceptor',errorInterceptor);
延长:
这是我用来拦截 $http
调用的片段 (所以它对我有用)
module MyModule
{
var app = angular.module("MyModule");
export class HttpErrorAspect
{
static $inject = ["$q"];
constructor(private $q: ng.IQService)
{
}
public responseError = (rejection: any): any =>
{
// do some magic, e.g. use toaster or alerter
// to notify about the issue
...
// reject that all
return this.$q.reject(rejection);
}
}
app.service("HttpErrorFilter", MyModule.HttpErrorAspect);
}
@service(app)
export class UserRolesService {
static $inject: string[] = ["$cookies"];
private $cookies: angular.cookies.ICookiesService;
user: IUser;
GetUser(): IUser {
return this.user;
}
}
致电:
this.userRolesService.GetUser()