有没有人尝试过在AngularJS HTML页面中使用typescript枚举? [英] Has anyone tried using typescript enums in AngularJS HTML pages?

查看:106
本文介绍了有没有人尝试过在AngularJS HTML页面中使用typescript枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在打字稿中,我创建了一个像这样的枚举:

In Typescript I created an enum like this:

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

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

In my controller I set a property called action like this:

class AuthService implements IAuthService {

    action: number;

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

        this.action = Action.None;

    }

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

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

This works good but how can I use the enum in my HTML. Is that possible? What I would like to do is to use it in some place like this:

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

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

Without the need to create different variables for:

app.authService.authenticating
app.authService.registering
...
etc

推荐答案

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

You can put that on the $rootScope e.g.

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

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

并且由于每个$scope都继承自它,因此它在每个作用域上,您可以执行Action.foo等.

And since every $scope inherits from it it is on each scope and you can just do Action.foo etc.

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

Note: For directives with an isolate scope you need to do $root.Action.foo. Just Action would not work as it intentionally breaks the prototype chain.

这篇关于有没有人尝试过在AngularJS HTML页面中使用typescript枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆