Angular2:是否可以在html模板中使用自定义类型? [英] Angular2: Is it possible to use custom types in html templates?

查看:120
本文介绍了Angular2:是否可以在html模板中使用自定义类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个枚举

export enum CustomPages{
  page1 = 1,
  page2 = 2,
  page3 = 3
}

在html模板中,我想访问该枚举.

In html template I want to access this enum.

例如:

<a class="nav-link" [class.active]="page == CustomPages.page1" href="#">Categories and responses</a>

它引发异常

无法读取未定义的属性"page1"

Cannot read property 'page1' of undefined

推荐答案

您需要在组件内部定义 CustomPages .

You need to define CustomPages inside your component.

import {CustomPages} from 'path/to/enum/CustomPages';

@Component({...})
export class YourComponentName {

    CustomPages = CustomPages; // <- assign enum to the same name as a field.

    constructor(..){..}

    ...

}

这篇关于Angular2:是否可以在html模板中使用自定义类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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