1

How to create an TypeScript static constant in a class? That mean I can refer a constant without initialize the class. This is my class:

export class CallTree{
    public static readonly active = 1;
    ............................
}

The component's html is something like the following:

 <table mat-table [dataSource]="callTreeList" matSort class="mat-elevation-z8 callTreeList">
    ...............................

  <ng-container matColumnDef="status">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>Status</th>
    <td mat-cell *matCellDef="let element" [innerHTML]="(element.status === CallTree.active)?'Active':'Inactive'"></td>
   </ng-container>

The browser console prompt the following error message:

TypeError: Cannot read property 'active' of undefined 

How can I fix the problem? How can I use this constant in the Angular component's html?

4
  • 2
    Where are you calling the console.log()? Commented Dec 23, 2019 at 4:31
  • I don't think your declaration of static const is wrong, as @LloydNicholson said, from where you are calling it might be wrong. Commented Dec 23, 2019 at 4:44
  • In the component's html. Commented Dec 23, 2019 at 4:54
  • 1
    can you post the code in question the code for component? Commented Dec 23, 2019 at 5:01

2 Answers 2

2

you can import the class and refer the class property in you components.

assume you have your constant class in the const.class.ts file

import { CallTree } from './const.class'

export class AppComponent  {

  name = CallTree.active;    

}
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer inspired me to find the solution.
0

You can declare like this

export class CallTree{

static readonly: number = 1;

}

https://www.tutorialsteacher.com/typescript/typescript-static

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.