我正在用 typescript 编写我的 Angular 应用程序。 为了防止冗余,我想完成某种类型的通用处理。 这就是我的出发点:
class BaseProvider {
api_url = 'http://localhost:80/api/FILL_OUT_PATH/:id';
$get($resource){
var provider = $resource(this.api_url, {}, {
update: {
method: 'PUT'
}
});
return provider;
}
}
和
class User extends BaseProvider{
constructor() {
super();
this.api_url = 'http://localhost:80/api/users/:id';
}
}
然后
module Controllers
{
export class BaseController {
message = "Base controller";
entity : any;
entities : any;
constructor($scope)
{
}
}
}
和
module Controllers
{
export class UserController extends BaseController {
name = "UserController";
constructor($scope, User)
{
super($scope);
this.entity = new User();
this.entities = User.query();
$scope.vm = this;
}
}
}
这是我想使用 UserController(P 代码)的地方:
module Controllers
{
export class UserController<T extends BaseProvider> extends BaseController {
name = "UserController";
static $inject = ['$scope', T.typename]; // Inject the types name somehow?
constructor($scope, entity)
{
super($scope);
this.entity = new T();
this.entities = T.query();
$scope.user = this;
}
}
typescript 中是否有处理此问题的工具?
最佳答案
Is there a facility in typescript to handle this?
没有。所有类型信息都从生成的 JS 中删除,因此您不能将泛型参数用作变量。
关于angularjs - 通用类型名称注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25212222/