question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] 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:

Current behavior

Templates cannot refer to anything not directly associated with the component, so you can’t for example do this:

<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:

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 the template, without needing to assign them to a component member. Maybe a way of importing variables/types into the HTML, or declaring types in the component that are available to the template. Something like this perhaps:

import { someConstant } from './somewhere';
import { UserStatus } from './somewhere';
import { isDevMode } from '@angular/core';

@Component({
  selector: 'app-user-list',
  templateUrl: './user-list.component.html',
  templateImports: [someConstant, UserStatus, isDevMode]
})

(this particular solution wouldn’t work for const enums, other solutions may be better)

Environment


Angular CLI: 6.1.2
Node: 8.11.1
OS: linux x64
Angular: 6.1.1

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:161
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

21reactions
trotylcommented, Sep 14, 2018
templateImports: [someConstant, UserStatus, isDevMode]

This would not work, but the below could:

templateImports: {someConstant, UserStatus, isDevMode}
6reactions
laurentgoudetcommented, Apr 6, 2021

Perhaps I am missing something here, but this seems like a pretty clean and intuitive approach.

That works well most of the time, but isn’t possible when using a const enum 😦

In addition, non-const enums bear the cost of extra generated code (and additional indirection when accessing enum values), which quickly adds up.

Using string unions solves that, but IMO enums are nicer because they namespace the values, makes the code easier to read (i.e. easier to find where the values are defined), and are a more familiar pattern for people coming from other languages.

For now we’re still using non-const enums but having Angular supports const ones would allow to shave off at least 150kB from our app according to the bundle sizes report, i.e. this issue has concrete performance implications for medium-to-large apps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c++ c99 template specialization with an Enum parameter ...
Basically, I am trying to use a enum which could do bit operations and there is a template function could map the enum...
Read more >
Angular Basics: Working With Enums in Angular - Telerik
We can define enums without assigning each member to a value. ... Since Angular projects use TypeScript, we can add enums into our...
Read more >
Handbook - Enums - TypeScript
Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a...
Read more >
Enum With and Without Values - Learn TypeScript - Educative.io
An enum is a structure that proposes several allowed values for a variable. It is a way to constrain variable values by defining...
Read more >
Enumeration declaration - cppreference.com
An enumeration is a distinct type whose value is restricted to a range of values (see below for details), which may include several ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found