gpt4 book ai didi

angularjs - typescript + AngularJS v1 : Rewriting angular factory to TypeScript

转载 作者:搜寻专家 更新时间:2023-10-30 20:41:15 26 4
gpt4 key购买 nike

我想知道如何将以下工厂重写为 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);
}

关于angularjs - typescript + AngularJS v1 : Rewriting angular factory to TypeScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33759671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com