我在用打字机写我的角度应用程序。
为了防止冗余,我想完成某种类型的通用处理。
我就是从这里来的:
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-code)的地方:
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中有处理这个问题的工具吗?
最佳答案
typescript中有处理这个问题的工具吗?
不。所有类型信息都将从生成的js中删除,因此不能将泛型参数用作变量。
关于angularjs - 通用类型名称注入(inject),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25212222/