Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: Is it possible to use custom types in html templates?

For example I have an enumeration

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

In html template I want to access this enum.

For example:

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

It throws an exception

Cannot read property 'page1' of undefined

like image 271
Grigor Aleksanyan Avatar asked Jan 01 '26 00:01

Grigor Aleksanyan


1 Answers

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(..){..}

    ...

}
like image 62
eko Avatar answered Jan 03 '26 21:01

eko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!