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

Sam*_*tar 9 angularjs typescript

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

enum Action { None = 0, Registering = 1, Authenticating = 2 };
Run Code Online (Sandbox Code Playgroud)

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

class AuthService implements IAuthService {

    action: number;

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

        this.action = Action.None;

    }

    doRegister() => {
       this.action = Action.Registering;
    }
Run Code Online (Sandbox Code Playgroud)

这很好,但我如何在HTML中使用枚举.那可能吗?我想做的是在这样的地方使用它:

<span ng-class="{'fa-spin fa-spinner': app.authService.authenticating }">
Run Code Online (Sandbox Code Playgroud)

无需为以下内容创建不同的变量:

app.authService.authenticating
app.authService.registering
...
etc
Run Code Online (Sandbox Code Playgroud)

bas*_*rat 11

你可以把它放在$rootScope例如

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

    // put it on $rootScope
    $rootScope.Action = Action;
});
Run Code Online (Sandbox Code Playgroud)

并且因为每个都$scope继承了它,它在每个范围上,你可以做Action.foo等等.

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