有没有人尝试在AngularJS HTML页面中使用typescript枚举?

时间:2014-08-25 06:02:18

标签: angularjs typescript

在Typescript中,我创建了一个这样的枚举:

enum Action { None = 0, Registering = 1, Authenticating = 2 };

在我的控制器中,我设置了一个名为action的属性:

class AuthService implements IAuthService {

    action: number;

    constructor(
        private $state,
        private userService,
        private utilityService: IUtilityService
        ) {

        this.action = Action.None;

    }

    doRegister() => {
       this.action = Action.Registering;
    }

这很好但我怎样才能在HTML中使用枚举。那可能吗?我想做的是在这样的地方使用它:

<span ng-class="{'fa-spin fa-spinner': app.authService.authenticating }">

无需为:

创建不同的变量
app.authService.authenticating
app.authService.registering
...
etc

1 个答案:

答案 0 :(得分:11)

您可以将其放在$rootScope上,例如

mainmodule.run([
    '$rootScope'
], function ($rootScope){

    // put it on $rootScope
    $rootScope.Action = Action;
});

由于每个$scope都继承了它,因此它位于每个范围内,你可以Action.foo等。

注意:对于具有隔离范围的指令,您需要执行$root.Action.foo。只是Action无法正常工作,因为它故意破坏了原型链。

相关问题