IssueRaiser

Allow constants, enums, functions to be used in templates without having to add component members

## I'm submitting a... <!-- Check one of the following options with "x" --> <pre><code> [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report <!-- Please search GitHub for a similar issue or PR before submitting --> [ ] Performance issue [ x ] Feature request [ ] Documentation issue or request [ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question [ ] Other... Please describe: </code></pre> ## Current behavior Templates cannot refer to anything not directly associated with the component, so you can't for example do this: ```html <div *ngIf="user.id === someConstant"> </div> <div *ngIf="user.status === UserStatus.Active"> <!-- UserStatus is an enum --> </div> <div *ngIf="isDevMode()"> </div> ``` To make this work, I have to declare these as members of my component: ```typescript import { someConstant } from './somewhere'; import { UserStatus } from './somewhere'; import { isDevMode } from '@angular/core'; export class MyComponent { public someConstant = someConstant; public UserStatus = UserStatus; public isDevMode = isDevMode; } ``` These can quickly clutter the component code, and can't be used for const enums. For const enums I have to duplicate the entire enum into a constant, which becomes a maintenance headache. ## Expected behavior It would be helpful if I could use constants, enums and functions directly from within t

Posted by pcroc on Sep 14, 2018, 9:27 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments

support (Software)

Allow constants, enums, functions to be used in templates without having to add component members

pcrocSep 14, 2018, 9:27 AM

## I'm submitting a... <!-- Check one of the following options with "x" --> <pre><code> [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report <!-- Please search GitHub for a similar issue or PR before submitting --> [ ] Performance issue [ x ] Feature request [ ] Documentation issue or request [ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question [ ] Other... Please describe: </code></pre> ## Current behavior Templates cannot refer to anything not directly associated with the component, so you can't for example do this: ```html <div *ngIf="user.id === someConstant"> </div> <div *ngIf="user.status === UserStatus.Active"> <!-- UserStatus is an enum --> </div> <div *ngIf="isDevMode()"> </div> ``` To make this work, I have to declare these as members of my component: ```typescript import { someConstant } from './somewhere'; import { UserStatus } from './somewhere'; import { isDevMode } from '@angular/core'; export class MyComponent { public someConstant = someConstant; public UserStatus = UserStatus; public isDevMode = isDevMode; } ``` These can quickly clutter the component code, and can't be used for const enums. For const enums I have to duplicate the entire enum into a constant, which becomes a maintenance headache. ## Expected behavior It would be helpful if I could use constants, enums and functions directly from within t

Comments (0)

No comments yet. Start the discussion.

Comment on this issue