有人试过在AngularJS HTML页面中使用typescript枚举吗?
在Typescript中,我创建了如下枚举:有人试过在AngularJS HTML页面中使用typescript枚举吗?,angularjs,typescript,Angularjs,Typescript,在Typescript中,我创建了如下枚举: enum Action { None = 0, Registering = 1, Authenticating = 2 }; class AuthService implements IAuthService { action: number; constructor( private $state, private userService, private utilityServ
enum Action { None = 0, Registering = 1, Authenticating = 2 };
class AuthService implements IAuthService {
action: number;
constructor(
private $state,
private userService,
private utilityService: IUtilityService
) {
this.action = Action.None;
}
doRegister() => {
this.action = Action.Registering;
}
在控制器中,我设置了一个名为action的属性,如下所示:
enum Action { None = 0, Registering = 1, Authenticating = 2 };
class AuthService implements IAuthService {
action: number;
constructor(
private $state,
private userService,
private utilityService: IUtilityService
) {
this.action = Action.None;
}
doRegister() => {
this.action = Action.Registering;
}
这很好,但是如何在HTML中使用枚举。可能吗?我想做的是在这样的地方使用它:
enum Action { None = 0, Registering = 1, Authenticating = 2 };
class AuthService implements IAuthService {
action: number;
constructor(
private $state,
private userService,
private utilityService: IUtilityService
) {
this.action = Action.None;
}
doRegister() => {
this.action = Action.Registering;
}
无需为以下各项创建不同的变量:
app.authService.authenticating
app.authService.registering
...
etc
您可以将其放在
$rootScope
上,例如
mainmodule.run([
'$rootScope'
], function ($rootScope){
// put it on $rootScope
$rootScope.Action = Action;
});
由于每个$scope
都继承自它,所以它位于每个作用域上,您只需执行Action.foo
等操作即可
注意:对于具有隔离作用域的指令,您需要执行
$root.Action.foo
。仅仅操作
不起作用,因为它故意破坏原型链 我也在检查常量的用法。你认为使用app.constant(或使用Typescript解决方案)更容易吗?你能告诉我如何将$rootScope注入控制器吗?我是注入$rootScope还是只注入$scope?两者都有。但你不需要。像任何其他不需要访问angular的ts实用程序函数一样使用enums